1 / 22

Doubly Linked List

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

nola-finch
Download Presentation

Doubly Linked List

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

  2. 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

  3. 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

  4. Doubly Linked List Definition #include <iostream> using namespace std; struct Node{ int data; Node* next; Node* prev; }; typedef Node* NodePtr;

  5. Doubly Linked List Operations • insertNode(NodePtr Head, int item) //add new node to ordered doubly linked list • deleteNode(NodePtr Head, int item) //remove a node from doubly linked list • searchNode(NodePtr Head, int item) • print(NodePtr Head, int item)

  6. 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

  7. 10 70 20 55 40 Inserting a Node • Insert a node New before Cur (not at front or rear) New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; Head Cur New

  8. Doubly Linked Lists 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

  9. Dummy Head Node 70 20 55 40 10 Head Doubly Linked Lists with Dummy Head • Non-Empty List • Empty List Dummy Head Node Head

  10. void createHead(NodePtr& Head){ Head = new Node; Head->next = Head; Head->prev = Head; }

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

  12. 70 10 20 55 40 Dummy Head Node Cur Head • Delete a node Cur in the middle (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur; // same as delete front!

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

  14. void deleteNode(NodePtr Head, int item){ NodePtr Cur; Cur = searchNode(Head, item); if(Cur != NULL){ Cur->prev->next = Cur->next; Cur->next->prev = Cur->prev; delete Cur; } }

  15. 20 10 Inserting a Node • 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

  16. 55 10 20 40 • 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

  17. 70 20 55 40 10 • 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

  18. 20 • 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

  19. void insertNode(NodePtr Head, int item){ NodePtr 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; }

  20. Searching a node: NodePtr searchNode(NodePtr Head, int item){ NodePtr Cur = Head->next; while(Cur != Head){ if(Cur->data == item) return Cur; if(Cur->data < item) Cur = Cur->next; else break; } return NULL; }

  21. Print the whole list: void print(NodePtr Head){ NodePtr Cur=Head->next; while(Cur != Head){ cout << Cur->data << " "; Cur = Cur->next; } cout << endl; }

  22. 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 void main(){ NodePtr 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; }

More Related