Doubly Linked List COMP 104 Doubly Linked Lists

  • Slides: 22
Download presentation
Doubly Linked List

Doubly Linked List

COMP 104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a

COMP 104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor prev points to the predecessor n next points to the successor n 10 Head 20 Cur->prev 40 Cur 55 Cur->next 70

COMP 104 Doubly Linked Lists / Slide 3 Motivation * Doubly linked lists are

COMP 104 Doubly Linked Lists / Slide 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

Doubly Linked List Definition #include <iostream> using namespace std; struct Node{ int data; Node*

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

COMP 104 Doubly Linked Lists / Slide 5 Doubly Linked List Operations * insert.

COMP 104 Doubly Linked Lists / Slide 5 Doubly Linked List Operations * insert. Node(Node. Ptr Head, int item) //add new node to ordered doubly linked list * delete. Node(Node. Ptr Head, int item) //remove a node from doubly linked list * search. Node(Node. Ptr Head, int item) * print(Node. Ptr Head, int item)

COMP 104 Doubly Linked Lists / Slide 6 Deleting a Node * Delete a

COMP 104 Doubly Linked Lists / Slide 6 Deleting a Node * Delete a node Cur (not at front or rear) (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur; 10 20 40 Head Cur 55 70

COMP 104 Doubly Linked Lists / Slide 7 Inserting a Node * Insert a

COMP 104 Doubly Linked Lists / Slide 7 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; 10 Head 20 55 40 Cur New 70

Doubly Linked Lists with Dummy Head Node COMP 104 Doubly Linked Lists / Slide

Doubly Linked Lists with Dummy Head Node COMP 104 Doubly Linked Lists / Slide 8 * 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

COMP 104 Doubly Linked Lists / Slide 9 n Doubly Linked Lists with Dummy

COMP 104 Doubly Linked Lists / Slide 9 n Doubly Linked Lists with Dummy Head Non-Empty List Dummy Head Node 10 20 40 Head n Empty List Dummy Head Node Head 55 70

void create. Head(Node. Ptr& Head){ Head = new Node; Head->next = Head; Head->prev =

void create. Head(Node. Ptr& Head){ Head = new Node; Head->next = Head; Head->prev = Head; }

COMP 104 Doubly Linked Lists / Slide 11 Deleting a Node * Delete a

COMP 104 Doubly Linked Lists / Slide 11 Deleting a Node * Delete a node Cur at front (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur; Dummy Head Node 10 Head Cur 20 40 55 70

COMP 104 Doubly Linked Lists / Slide 12 * Delete a node Cur in

COMP 104 Doubly Linked Lists / Slide 12 * Delete a node Cur in the middle (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur; // same as delete front! Dummy Head Node 10 Head 20 40 Cur 55 70

COMP 104 Doubly Linked Lists / Slide 13 * Delete a node Cur at

COMP 104 Doubly Linked Lists / Slide 13 * 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 10 Head 20 40 55 70 Cur

void delete. Node(Node. Ptr Head, int item){ Node. Ptr Cur; Cur = search. Node(Head,

void delete. Node(Node. Ptr Head, int item){ Node. Ptr Cur; Cur = search. Node(Head, item); if(Cur != NULL){ Cur->prev->next = Cur->next; Cur->next->prev = Cur->prev; delete Cur; } }

COMP 104 Doubly Linked Lists / Slide 15 Inserting a Node * Insert a

COMP 104 Doubly Linked Lists / Slide 15 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 20 10 Head New Cur

COMP 104 Doubly Linked Lists / Slide 16 * Insert a Node New in

COMP 104 Doubly Linked Lists / Slide 16 * 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 10 Head 20 40 New 55 Cur

COMP 104 Doubly Linked Lists / Slide 17 * Insert a Node New at

COMP 104 Doubly Linked Lists / Slide 17 * 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 10 Cur Head 20 40 55 70 New

COMP 104 Doubly Linked Lists / Slide 18 * Insert a Node New to

COMP 104 Doubly Linked Lists / Slide 18 * Insert a Node New to Empty List (with Cur pointing to dummy head node) New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; Dummy Head Node 20 Head Cur New

void insert. Node(Node. Ptr Head, int item){ Node. Ptr New, Cur; New = new

void insert. Node(Node. Ptr Head, int item){ Node. Ptr 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; }

Searching a node: Node. Ptr search. Node(Node. Ptr Head, int item){ Node. Ptr Cur

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

Print the whole list: void print(Node. Ptr Head){ Node. Ptr Cur=Head->next; while(Cur != Head){

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

void main(){ Node. Ptr Head, temp; Result is: create. Head(Head); print(Head); insert. Node(Head, 3);

void main(){ Node. Ptr Head, temp; Result is: create. Head(Head); print(Head); insert. Node(Head, 3); 3 print(Head); insert. Node(Head, 5); 3 5 print(Head); 2 3 5 insert. Node(Head, 2); print(Head); 1 2 3 5 7 8 insert. Node(Head, 7); 1 2 3 5 8 insert. Node(Head, 1); insert. Node(Head, 8); Data is contained in the print(Head); delete. Node(Head, 7); delete. Node(Head, 0); print(Head); temp = search. Node(Head, 5); if(temp != NULL) cout << "Data is contained in the list" << endl; else cout << "Data is NOT contained in the list" << endl; } list