1 / 64

CSC 211 Data Structures Lecture 21

CSC 211 Data Structures Lecture 21. Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk. 1. Last Lecture Summary. Comparison of Merge Sort and Quick Sort Shell Sort Concept, Examples, Algorithm, Complexity Radix Sort Concept, Examples, Algorithm, Complexity Bucket Sort

arty
Download Presentation

CSC 211 Data Structures Lecture 21

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. Objectives Overview • Doubly Linked List • Concept • Operations on Doubly Linked List • Insertion • Deletion • Traversing • Search • Implementation Code • Doubly Linked List with Two Pointers

  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. 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 andconcatenation, are also used

  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. Linked List • Pointer Based Implementation of Linked List ADT • Dynamically allocateddata 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. 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. 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 nodein the list, e.g. NULL Head a) The headis a pointerto 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 outover the memory.

  10. 2 Linked List • Actual picture in memory: 1051 6 1052 1063 current 1053 1063 1054 2 head 1055 1051 1056 6 8 7 1 1057 7 1058 1060 current 1059 1060 1 1061 0 head 1062 1054 1063 8 1064 1057 1065

  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 - • structListNode { int data; ListNode *next; };

  12. List Declarations a) The first member of the ListNodestruct is a intcalled data. It is to hold the node’sdata. b) The second member is a pointer called next. It is to hold the address of any object that is a ListNodestruct. Hence each ListNodestruct can point to the next one in the list. The ListNodestruct 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. 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. structListNode {int data;ListNode *next; }; ListNode *head; // List head pointer

  14. Head 87 34 78 65 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)

  15. nodePtr Head 87 98 67 Traversing a SLL (animation)

  16. nodePtr 78 Head 67 93 56 Inserting after (animation) 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. 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. Head 67 93 56 (predecessor) Head 67 93 56 Deleting an element from a SLL • To delete the first element, change the link in the header • To delete some other element, change the link in its predecessor

  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. 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. Doubly Linked List 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 … … … …

  22. 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 56 67 93 Doubly-Linked Lists (DLL)

  23. 10 70 55 40 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 20 Head Cur->prev Cur->next Cur

  24. 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 DLLs compared to SLLs

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

  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. 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. DLL - Insertion

  29. 10 70 20 55 40 Inserting a Node • Insert a node NewNodebefore Cur (not at front or rear) NewNode->next = Cur; NewNode->prev = Cur->prev; Cur->prev = NewNode; (NewNode->prev)->next = Newnode; Head Cur NewNode

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

  31. 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 56 67 93 Deleting a node from a DLL

  32. 10 70 20 55 40 Deleting a Node • Delete a node Cur (not at front or rear) (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur; Head Cur

  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 nth element • you have to count your way through a lot of other elements

  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. Dummy Head Node 70 20 55 40 10 Head Doubly Linked Lists with Dummy Head • Non-Empty List • Empty List Dummy Head Node Head

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

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

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

  39. 55 10 20 40 Inserting a Node – in the Middle • 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! Dummy Head Node New Cur Head

  40. 70 20 55 40 10 Inserting a Node at Rear • 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! Dummy Head Node New Cur Head

  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. 70 20 55 40 10 Deleting a Node – at Head • Delete a node Cur at front (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur; Dummy Head Node Head Cur

  43. 70 10 20 55 40 Deleting a Node – in the Middle • 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 Dummy Head Node Cur Head

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

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

  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. 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. Doubly Linked List with Two Pointers With two Pointers One for Head Another for Rear c head tail head = new Node (); tail = new Node (); head->next = tail; tail->prev = head;

  50. Inserting into a Doubly Linked List a c head tail current newNode = newNode; newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

More Related