doubly linked list. comp104 doubly linked lists / slide 2 doubly linked lists * in a doubly linked...
TRANSCRIPT
![Page 1: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/1.jpg)
Doubly Linked List
![Page 2: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/2.jpg)
COMP104 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 next points to the successor
10 7020 5540
HeadCur Cur->nextCur->prev
![Page 3: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/3.jpg)
COMP104 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
![Page 4: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/4.jpg)
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
Node* prev;
};
typedef Node* NodePtr;
Doubly Linked List Definition
![Page 5: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/5.jpg)
COMP104 Doubly Linked Lists / Slide 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)
![Page 6: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/6.jpg)
COMP104 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 7020 5540
HeadCur
![Page 7: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/7.jpg)
COMP104 Doubly Linked Lists / Slide 7
Inserting a Node
Insert a node New before Cur (not at front or rear)
10 7020 55
40Head
New
Cur
New->next = Cur;
New->prev = Cur->prev;
Cur->prev = New;
(New->prev)->next = New;
![Page 8: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/8.jpg)
COMP104 Doubly Linked Lists / Slide 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
![Page 9: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/9.jpg)
COMP104 Doubly Linked Lists / Slide 9
Doubly Linked Lists with Dummy Head
Non-Empty List
Empty List
7020 5540
Head
10Dummy Head Node
Head
Dummy Head Node
![Page 10: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/10.jpg)
void createHead(NodePtr& Head){
Head = new Node;
Head->next = Head;
Head->prev = Head;
}
![Page 11: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/11.jpg)
COMP104 Doubly Linked Lists / Slide 11
Deleting a Node
Delete a node Cur at front
7020 5540
Head
10Dummy Head Node
Cur
(Cur->prev)->next = Cur->next;
(Cur->next)->prev = Cur->prev;
delete Cur;
![Page 12: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/12.jpg)
COMP104 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!
70
Head
10Dummy Head Node
20 5540
Cur
![Page 13: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/13.jpg)
COMP104 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!
7020 5540
Head
10Dummy Head Node
Cur
![Page 14: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/14.jpg)
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;
}
}
![Page 15: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/15.jpg)
COMP104 Doubly Linked Lists / Slide 15
Inserting a Node 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
![Page 16: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/16.jpg)
COMP104 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!
55
Head
10Dummy Head Node
20
Cur
40
New
![Page 17: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/17.jpg)
COMP104 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!
7020 5540
Head
10Dummy Head Node
Cur New
![Page 18: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/18.jpg)
COMP104 Doubly Linked Lists / Slide 18
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
![Page 19: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/19.jpg)
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;
}
![Page 20: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/20.jpg)
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;
}
Searching a node:
![Page 21: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/21.jpg)
void print(NodePtr Head){
NodePtr Cur=Head->next;
while(Cur != Head){
cout << Cur->data << " ";
Cur = Cur->next;
}
cout << endl;
}
Print the whole list:
![Page 22: Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor](https://reader036.vdocuments.us/reader036/viewer/2022081504/56649cb95503460f94981175/html5/thumbnails/22.jpg)
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;}
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