Data Structure Dr. Mohamed Khafagy.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CHP-5 LinkedList.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
M180: Data Structures & Algorithms in Java
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Data Structures: A Pseudocode Approach with C
CS Data Structures Chapter 4 Lists. Dynamically Linked Stacks and Queues (1/8)  When several stacks and queues coexisted, there was no efficient.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
CS Data Structures Chapter 4 Lists.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
Reference: Vinu V Das, Principles of Data Structures using C and C++
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Linked List, Stacks Queues
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Data Structure By Amee Trivedi.
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
More Linking Up with Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
UNIT-3 LINKED LIST.
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Chapter 4 Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
Data Structures 7th Week
Lists.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Lists.
Arrays and Linked Lists
Stacks, Queues, and Deques
Linked List (Part I) Data structure.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Further Data Structures
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
CS212D: Data Structures Week 5-6 Linked List.
Problem Understanding
Chapter 16 Linked Structures
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Data Structures & Algorithms
CS210- Lecture 6 Jun 13, 2005 Announcements
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
BY PROF. IRSHAD AHMAD LONE.
LINEAR DATA STRUCTURES
Problem Understanding
Presentation transcript:

Data Structure Dr. Mohamed Khafagy

Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive nodes of data object were stored a fixed distance apart. The drawback of sequential mapping for ordered lists is that operations such as insertion and deletion become expensive. Also sequential representation tends to have less space efficiency when handling multiple various sizes of ordered lists.

Linked List A better solutions to resolve the aforementioned issues of sequential representations is linked lists. Elements in a linked list are not stored in sequential in memory. Instead, they are stored all over the memory. They form a list by recording the address of next element for each element in the list. Therefore, the list is linked together. A linked list has a head pointer that points to the first element of the list. By following the links, you can traverse the linked list and visit each element in the list one by one.

Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node next node elem  A B C D

Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next

Layout of L = (a,b,c,d,e) using an array representation. Memory Layout Layout of L = (a,b,c,d,e) using an array representation. a b c d e The figures show computer memory. An array uses a contiguous chunk of memory. A linked representation uses an arbitrary layout. c a e d b

Linked Representation pointer (or link) in e is NULL c a e d b first use a variable first to get to the first element a

Normal Way To Draw A Linked List b c d e NULL first link or pointer field of node data field of node

Chain first NULL a b c d e A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a NULL (or 0) pointer.

Node Representation template <class T> class ChainNode { private: T data; ChainNode<T> *link;   // constructors come here }; link data Better to define as a struct than a class as otherwise need to make every using class a friend so that node fields may be accessed.

Insertion To insert a new data value into a linked list, we must first obtain a new node and store the value in its data part The second step is to connect this new node to existing list Two cases in this situation: (1) insertion after some element in the list and (2) insertion at the beginning of the list

Inserting at the Head Allocate a new node update new element Have new node point to old head Update head to point to new node

predptr points to the node containing 17 Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr

Insertion Note: insertion also works at end of list pointer member of new node set to null Insertion at the beginning of the list predptr must be set to first pointer member of newptr set to that value (Where first points to) first set to value of newptr In all cases, no shifting of list elements is required !

Insert X immediately after current position b c d current Insert X immediately after current position a b c d x current

Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); a b current tmp

Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; a b x current tmp

Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; // x’s next node is b tmp.next = current.next; a b x current tmp

Implementing Insertion: Step By Step Insertion immediately after current position // create a new node tmp = new ListNode<DataType>(); // place x in the element field tmp.data = x; // x’s next node is b tmp.next = current.next; // a’s next node is x current.next = tmp; a b x current tmp

Inserting at the Tail Allocate a new node Insert new element Have new node point to null Have old last node point to new node Update tail to point to new node

Deletion For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list

Removing at the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node

Removing at the Tail Removing at the tail of a singly linked list is not efficient! There is no constant- time way to update the tail to point to the previous node

Deletion Delete node containing 22 from list. Do a bypass operation: predptr ptr To free space Delete node containing 22 from list. Suppose ptr points to the node to be deleted predptr points to its predecessor (the 17) Do a bypass operation: Set the next pointer in the predecessor to point to the successor of the node to be deleted Deallocate the node being deleted.

Deletion The second case is easier Just set the first points to the second node in the list and then returning the deleted node to the storage pool

Implementing Basic Deletion Delete an item immediately after current position Basic deletion is a bypass in the linked list. a x b current a b current

Implementing Basic Deletion Need a reference to node prior to the one to be deleted. current.next = current.next.next; a x b current a x b current a b current

The Scenario Begin with an existing linked list Could be empty or not head 6 42 4 17 Begin with an existing linked list Could be empty or not Could be ordered or not

The Scenario Begin with an existing linked list Could be empty or not head 6 42 4 17 Begin with an existing linked list Could be empty or not Could be ordered or not

The Scenario Begin with an existing linked list Could be empty or not head 42 4 17 Begin with an existing linked list Could be empty or not Could be ordered or not

The Scenario Begin with an existing linked list Could be empty or not head 42 4 17 Begin with an existing linked list Could be empty or not Could be ordered or not

Finding the Match We’ll examine three situations: Delete the first element Delete the first occurrence of an element Delete all occurrences of a particular element

Deleting the First Element This can be done without any traversal/searching Requires an in/out pointer procedure DeleteFront (current iot in/out ptr toa Node) // deletes the first node in the list if (current <> nil) then current <- current^.next endif endprocedure

Deleting the First Element This can be done without any traversal/searching Requires an in/out pointer procedure DeleteFront (current iot in/out ptr toa Node) // deletes the first node in the list if (current <> nil) then current <- current^.next endif endprocedure

Deleting from a Linked List Deletion from a linked list involves two steps: Find a match to the element to be deleted (traverse until nil or found) Perform the action to delete Performing the deletion is trivial: current <- current^.next This removes the element, since nothing will point to the node.

head 6 42 4 17 . Delete(head, 4)

head 6 42 4 17

head 6 42 4 17

head 6 42 4 17

head 6 42 4 17

head 6 42 4 17

head 6 42 4 17

head 6 42 4 17

head 6 42 4 17

head 6 42 4 17

head 6 42 17

Deleting All Occurrences Deleting all occurrences is a little more difficult. Traverse the entire list and don’t stop until you reach nil. If you delete, recurse on current If you don’t delete, recurse on current^.next

Applications of Linked Lists Stacks and Queues Implemented with Linked Lists Polynomials Implemented with Linked Lists Remember the array based implementation? Hint: two strategies, one efficient in terms of space, one in terms of running time

Linked Stacks and Queues top front rear Linked Queue Linked Stack

Stack with a Singly Linked List We can implement a stack with a singly linked list The top element is stored at the first node of the list The space used is O(n) and each operation of the Stack ADT takes O(1) time nodes t  elements

Queue with a Singly Linked List We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time r nodes f  elements

Array-Based Implementation of Linked Lists Given a list with names Implementation would look like this

How to do it??? First of all, define a 2D array int array[10][2];

How to do it??? Constructor( ) we make “first” variable equals to 7 to indicate we start with position 7 first ==7; How to choose a start point??

How to do it??? Put 88 into data position and set next position to NULL array[first][0]=88; array[first][1]=-1; size==1;

How to do Insertion??? Find a empty position (in this example it’s node 1) Insert(array, 1) { ptr==1; // find a empty position pre_ptr==7; // you need a function to find pre_ptr array[new_node][0]==54; array[new_node][1]==array[pre_ptr][1]; array[pre_ptr][1]==ptr; size++; }

Insertion Insertion predptr points to the node containing 17 To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr

How to do Deletion???

Implementation Details For Insertion, there are also two cases to consider: insertion after some element in the list and insertion at the beginning of the list For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list

Polynomials Representation typedef struct poly_node *poly_pointer; int coef; int expon; poly_pointer next; }; poly_pointer a, b, c; coef expon link

Example a 3 14 2 8 1 0 null b 8 14 -3 10 10 6 null

Adding Polynomials 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 a->expon == b->expon d 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 -3 10 a->expon < b->expon d

Adding Polynomials (cont’d) 3 14 2 8 1 0 a 8 14 -3 10 10 6 b 11 14 -3 10 2 8 d a->expon > b->expon

Adding Polynomials (cont’d) poly_pointer padd(poly_pointer a, poly_pointer b) { poly_pointer front, rear, temp; int sum; rear =(poly_pointer)malloc(sizeof(poly_node)); if (IS_FULL(rear)) { fprintf(stderr, “The memory is full\n”); exit(1); } front = rear; while (a && b) { switch (COMPARE(a->expon, b->expon)) {

case -1: /* a->expon < b->expon */ attach(b->coef, b->expon, &rear); b= b->next; break; case 0: /* a->expon == b->expon */ sum = a->coef + b->coef; if (sum) attach(sum,a->expon,&rear); a = a->next; b = b->next; case 1: /* a->expon > b->expon */ attach(a->coef, a->expon, &rear); a = a->next; } for (; a; a = a->next) for (; b; b=b->next) rear->next = NULL; temp = front; front = front->next; free(temp); return front;

Doubly Linked List Keep a pointer to the next and the previous element in the list typedef struct node *pnode; typedef struct node { char data [4]; pnode next; pnode prev; }

Doubly Linked List Keep a header and trailer pointers (sentinels) with no content header.prev = null; header.next = first element trailer.next = null; trailer.prev = last element Update pointers for every operation performed on the list How to remove an element from the tail of the list ?

Doubly Linked List – removeLast() Running time? How does this compare to simply linked lists?

Doubly Linked List insertFirst swapElements