Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.

Similar presentations


Presentation on theme: "1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1."— Presentation transcript:

1 1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

2 2 Last Lecture Summary Comparison of Merge Sort and Quick Sort Shell Sort  Concept, Examples, Algorithm, Complexity Radix Sort  Concept, Examples, Algorithm, Complexity Bucket Sort  Concept, Examples, Algorithm, Complexity Comparison of Sorting Techniques 2

3 3 Objectives Overview Doubly Linked List Concept Operations on Doubly Linked List  Insertion  Deletion  Traversing  Search Implementation Code Doubly Linked List with Two Pointers

4 4 Data Structure Operations Following are the major operations: Traversing: Accessing each record exactly once so that certain items in the record may be processed. (This accessing and processing is sometimes called "visiting" the record.) Searching: Finding the location of the record with a given key value, or finding the locations of all records that satisfy one or more conditions Inserting: Adding a new record to the structure Deleting: Removing a record from the structure

5 5 Data Structure Operations (Cont…) Sometimes two or more of the operations may be used in a given situation;  e.g., we may want to delete the record with a given key, which may mean we first need to search for the location of the record. Following two operations, which are used in special situations, are also be considered: Sorting: Arranging the records in some logical order  (e.g., alphabetically according to some NAME key, or in numerical order according to some NUMBER key, such as social security number or account number) Merging: Combining the records in two different sorted files into a single sorted file Other operations, e.g., copying and concatenation, are also used

6 6 List Using Linked Memory Various cells of memory are not allocated consecutively in memory. Not enough to store the elements of the list. With arrays, the second element was right next to the first element. Now the first element must explicitly tell us where to look for the second element. Do this by holding the memory address of the second element

7 7 Linked List Pointer Based Implementation of Linked List ADT Dynamically allocated data structures can be linked together to form a chain. A linked list is a series of connected nodes (or links) where each node is a data structure. A linked list can grow or shrink in size as the program runs. This is possible because the nodes in a linked list are dynamically allocated

8 8 Composition of Linked List Each node in the linked list contains –  a) One or more members that represent data (e.g. inventory records, customer names, addresses, telephone numbers, etc).  b) A pointer, that can point to another node. Data Members Pointer

9 9 Composition of linked List A linked list is called “linked” because each node in the series (i.e. the chain) has a pointer to the next node in the list, e.g. Head NULL a) The head is a pointer to the first node in the list. b) Each node in the list points to the next node in the list. c) The last node points to NULL (the usual way to signify the end). Note, the nodes in a linked list can be spread out over the memory.

10 10 Linked List  Actual picture in memory: 1051 1052 1055 1059 1060 1061 1062 1063 1064 1056 1057 1058 1053 10542 6 8 7 1 1051 1063 1057 1060 0 head1054 1063current 26871 head current 1065

11 11 List Declarations How to declare a linked list in C or C++? Step 1) Declare a data structure for the nodes. e.g. the following struct could be used to create a list where each node holds a float - struct ListNode { int data; ListNode *next; };

12 12 List Declarations a) The first member of the ListNode struct is a int called data. It is to hold the node’s data. b) The second member is a pointer called next. It is to hold the address of any object that is a ListNode struct. Hence each ListNode struct can point to the next one in the list. The ListNode struct contains a pointer to an object of the same type as that being declared. It is called a self-referential data structure. This makes it possible to create nodes that point to other nodes of the same type.

13 13 List Declarations Step 2) Declare a pointer to serve as the list head, e.g List *head; Before you use the head pointer, make sure it is initialized to NULL, so that it marks the end of the list. Once you have done these 2 steps (i.e. declared a node data structure, and created a NULL head pointer, you have an empty linked list. struct ListNode { int data; ListNode *next; }; ListNode *head;// List head pointer

14 14 Singly Linked List (SLL) - More Terminology A node’s successor is the next node in the sequence  The last node has no successor A node’s predecessor is the previous node in the sequence  The first node has no predecessor A list’s length is the number of elements in it  A list may be empty (contain no elements) 87347865 Head

15 15 Traversing a SLL (animation) 9887 67 Head nodePtr

16 16 Inserting after (animation) 9367 56 Head 78 nodePtr Find the node you want to insert after First, copy the link from the node that's already in the list Then, change the link in the node that's already in the list

17 17 Deleting a node from a SLL In order to delete a node from a SLL, you have to change the link in its predecessor This is slightly tricky, because you can’t follow a pointer backwards Deleting the first node in a list is a special case, because the node’s predecessor is the list header

18 18 Deleting an element from a SLL 9367 56 Head 9367 56 Head To delete the first element, change the link in the header To delete some other element, change the link in its predecessor (predecessor)

19 19 Doubly Linked List (DLL) In a singly linked list (SLL) one can move beginning from the head node to any node in one direction only (from left to right) SLL is also termed as one –way list On the other hand, Doubly Linked List (DLL) is a two-way list  One can move in either direction from left to right and from right to left This is accomplished by maintaining two linked fields instead of one as in a SLL

20 20 Motivation Doubly linked lists are useful for playing video and sound files with “rewind” and “instant replay” They are also useful for other linked data which require “rewind” and “fast forward” of the data

21 21 Each node on a list has two pointers.   A pointer to the next element.   A pointer to the previous element   The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list … … … … Doubly Linked List

22 22 Doubly-Linked Lists (DLL) Here is a doubly-linked list (DLL): Each node contains a value, a link to its successor (if any), and a link to its predecessor (if any) The header points to the first node in the list and to the last node in the list (or contains null links if the list is empty) Head 5667 93

23 23 Doubly Linked Lists In a Doubly Linked List each item points to both its predecessor and successor  prev points to the predecessor  next points to the successor 1070 20 5540 Head Cur Cur->nextCur->prev

24 24 DLLs compared to SLLs Advantages:  Can be traversed in either direction (may be essential for some programs)  Some operations, such as deletion and inserting before a node, become easier Disadvantages:  Requires more space to store backward pointer  List manipulations are slower because more links must be changed  Greater chance of having bugs because more links must be manipulated

25 25 Double Linked List – Definition in C struct Node{ int data; Node* next; Node* prev; } *Head, *nodePtr;

26 26 Operations on DLL The two node links allow traversal of the list in either direction While adding or removing a node in a doubly linked list requires changing more links than the same operations on a singly linked list The operations are simpler and potentially more efficient (for nodes other than first nodes)  because there is no need to keep track of the previous node during traversal or  no need to traverse the list to find the previous node, so that its link can be modified

27 27 Doubly Linked List Operations * * insertNode(Node *Head, int item) //add new node to ordered doubly linked list * * deleteNode(Node *Head, int item) //remove a node from doubly linked list * * searchNode(Node *Head, int item) * * print(Node *Head, int item)

28 28 DLL - Insertion

29 29 Inserting a Node Insert a node NewNode before Cur (not at front or rear) 1070205540 Head NewNode Cur NewNode->next = Cur; NewNode->prev = Cur->prev; Cur->prev = NewNode; (NewNode->prev)->next = Newnode;

30 30 DELETE: Pointer change for implementation of a Deletion Doubly – Linked List

31 31 Deleting a node from a DLL Node deletion from a DLL involves changing two links In this example, we will delete node 67 We don’t have to do anything about the links in node 67 Deletion of the first node or the last node is a special case Head 5667 93

32 32 Deleting a Node Delete a node Cur (not at front or rear) (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur; 1070205540 Head Cur

33 33 Other operations on linked lists Most “algorithms” on linked lists  such as insertion, deletion, and searching—are pretty obvious;  you just need to be careful Sorting a linked list is just messy,  since you can’t directly access the n th element  you have to count your way through a lot of other elements

34 34 DLL with Dummy Head Node To simplify insertion and deletion by avoiding special cases of deletion and insertion at front and rear, a dummy head node is added at the head of the list The last node also points to the dummy head node as its successor

35 35 Doubly Linked Lists with Dummy Head  Non-Empty List  Empty List 70205540 Head 10 Dummy Head Node Head Dummy Head Node

36 36 DLL– Creating Dummy Node at Head void createHead(Node *Head){ Head = new Node; Head->next = Head; Head->prev = Head; } Head Dummy Head Node

37 37 Insert a Node New to Empty List (with Cur pointing to dummy head node) Head Dummy Head Node New 20 New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; Cur Inserting a Node as First Node

38 38 Inserting a Node at Head Insert a Node New after dummy node and before Cur Head Dummy Head Node Cur 20 New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; 10 New

39 39 Insert a Node New in the middle and before Cur New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; // same as insert front! 55 Head 10 Dummy Head Node 20 Cur 40 New Inserting a Node – in the Middle

40 40 Insert a Node New at Rear (with Cur pointing to dummy head) New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; // same as insert front! 70205540 Head 10 Dummy Head Node Cur New Inserting a Node at Rear

41 41 DLL-Insert – Implementation Code void insertNode(Node *Head, int item){ Node *New, *Cur; New = new Node; New->data = item; Cur = Head->next; while(Cur != Head){//position Cur for insertion if(Cur->data < item) Cur = Cur->next; else break; } New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; }

42 42 Deleting a Node – at Head Delete a node Cur at front 70205540 Head 10 Dummy Head Node Cur (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur;

43 43 Delete a node Cur in the middle void deleteNode(Node *Head, int item){ Node *Cur; Cur = searchNode(Head, item); if(Cur != NULL){ Cur->prev->next = Cur->next; Cur->next->prev = Cur->prev; delete Cur; } // end of if } // end of function 70 Head 10 Dummy Head Node 205540 Cur Deleting a Node – in the Middle

44 44 Delete a node Cur at rear (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur;// same as delete front and middle! 70205540 Head 10 Dummy Head Node Cur Deleting a Node – at end

45 45 DLL – Searching a Node Node* searchNode(Node *Head, int item){ Node *Cur = Head->next; while(Cur != Head){ if(Cur->data == item) return Cur; if(Cur->data < item) Cur = Cur->next; else break; } return NULL; }

46 46 DLL – Printing the Whole List void print(Node *Head){ Node *Cur=Head->next; while(Cur != Head){ cout data << " "; Cur = Cur->next; } cout << endl; }

47 47 Main Program void main(){ Node *Head, *temp; createHead(Head); print(Head); insertNode(Head, 3); print(Head); insertNode(Head, 5); print(Head); insertNode(Head, 2); print(Head); insertNode(Head, 7); insertNode(Head, 1); insertNode(Head, 8); print(Head); deleteNode(Head, 7); deleteNode(Head, 0); print(Head); temp = searchNode(Head, 5); if(temp != NULL) cout << "Data is contained in the list" << endl; else cout << "Data is NOT contained in the list" << endl; } Result is: 3 3 5 2 3 5 1 2 3 5 7 8 1 2 3 5 8 Data is contained in the list

48 48 Doubly Linked List – Worst Case insertion at head or tail is in O(1) deletion at either end is on O(1) element access is still in O(n) 48

49 49 c head tail head = new Node (); tail = new Node (); head->next = tail; tail->prev = head; Doubly Linked List with Two Pointers With two Pointers One for Head Another for Rear

50 50 newNode = new Node; newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode Inserting into a Doubly Linked List ac head tail current

51 51 ac head tail newNode = new Node() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode b current Inserting into a Doubly Linked List

52 52 ac head tail newNode = new Node() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode b current Inserting into a Doubly Linked List

53 53 ac head tail newNode = new Node() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode b current Inserting into a Doubly Linked List

54 54 ac head tail newNode = new Node() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode b current Inserting into a Doubly Linked List

55 55 ac head tail newNode = new Node() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode b current Inserting into a Doubly Linked List

56 56 newNode = new Node() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode ac head tail b current Inserting into a Doubly Linked List

57 57 Deleting from a Doubly Linked List oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode; ac head b current tail

58 58 Deleting from a Doubly Linked List ac head b current oldNode tail oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

59 59 Deleting from a Doubly Linked List ac head b current oldNode tail oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

60 60 ac head b current oldNode tail oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode; Deleting from a Doubly Linked List

61 61 ac head b current oldNode tail oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode; Deleting from a Doubly Linked List

62 62 ac head current tail oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode; Deleting from a Doubly Linked List

63 63 abcd head Circular Linked lists Insertion and Deletion implementation left as an exercise

64 64 Summary Doubly Linked List Concept Operations on Doubly Linked List  Insertion  Deletion  Traversing  Search Implementation Code Doubly Linked List with Two Pointers  Insertion and Deletion


Download ppt "1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1."

Similar presentations


Ads by Google