Title: Doubly Linked List
1Programming
2Doubly 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-gtnext
Cur-gtprev
Cur
3Motivation
- 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
4Doubly Linked List Definition
- include ltiostreamgt
- using namespace std
- struct Node
- int data
- Node next
- Node prev
-
- typedef Node NodePtr
5Doubly 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)
6Deleting a Node
- Delete a node Cur (not at front or rear)
-
- (Cur-gtprev)-gtnext Cur-gtnext
- (Cur-gtnext)-gtprev Cur-gtprev
- delete Cur
Head
Cur
7Inserting a Node
- Insert a node New before Cur (not at front or
rear)
New-gtnext Cur New-gtprev Cur-gtprev Cur-gtprev
New (New-gtprev)-gtnext New
Head
Cur
New
8Doubly 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
9Doubly Linked Lists with Dummy Head
- Non-Empty List
-
- Empty List
Dummy Head Node
Head
10- void createHead(NodePtr Head)
- Head new Node
- Head-gtnext Head
- Head-gtprev Head
-
11Deleting a Node
- Delete a node Cur at front
(Cur-gtprev)-gtnext Cur-gtnext (Cur-gtnext)-gtprev
Cur-gtprev delete Cur
12- Delete a node Cur in the middle
(Cur-gtprev)-gtnext Cur-gtnext (Cur-gtnext)-gtprev
Cur-gtprev delete Cur // same as delete front!
13- Delete a node Cur at rear
(Cur-gtprev)-gtnext Cur-gtnext (Cur-gtnext)-gtprev
Cur-gtprev 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-gtprev-gtnext Cur-gtnext
- Cur-gtnext-gtprev Cur-gtprev
- delete Cur
-
-
15Inserting a Node
- Insert a Node New after dummy node and before Cur
New-gtnext Cur New-gtprev Cur-gtprev Cur-gtprev
New (New-gtprev)-gtnext New
Dummy Head Node
Head
Cur
New
16- Insert a Node New in the middle and before Cur
New-gtnext Cur New-gtprev Cur-gtprev Cur-gtprev
New (New-gtprev)-gtnext New // same as insert
front!
Dummy Head Node
Cur
New
Head
17- Insert a Node New at Rear (with Cur pointing to
dummy head)
New-gtnext Cur New-gtprev Cur-gtprev Cur-gtprev
New (New-gtprev)-gtnext New // same as insert
front!
Dummy Head Node
New
Head
Cur
18- Insert a Node New to Empty List (with Cur
pointing to dummy head node)
New-gtnext Cur New-gtprev Cur-gtprev Cur-gtprev
New (New-gtprev)-gtnext New
Dummy Head Node
Head
New
Cur
19- void insertNode(NodePtr Head, int item)
- NodePtr New, Cur
- New new Node
- New-gtdata item
-
- Cur Head-gtnext
- while(Cur ! Head) //position Cur for insertion
- if(Cur-gtdata lt item)
- Cur Cur-gtnext
- else
- break
-
- New-gtnext Cur
- New-gtprev Cur-gtprev
- Cur-gtprev New
- (New-gtprev)-gtnext New
-
20Searching a node
- NodePtr searchNode(NodePtr Head, int item)
- NodePtr Cur Head-gtnext
- while(Cur ! Head)
- if(Cur-gtdata item)
- return Cur
- if(Cur-gtdata lt item)
- Cur Cur-gtnext
- else
- break
-
- return NULL
-
21Print the whole list
- void print(NodePtr Head)
- NodePtr CurHead-gtnext
- while(Cur ! Head)
- cout ltlt Cur-gtdata ltlt " "
- Cur Cur-gtnext
-
- cout ltlt endl
-
22- 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)
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