5 Double Linked List and Its Operations
5 Double Linked List and Its Operations
UNIT-2
Double Linked List or Doubly Linked
List
Doubly linked list (or) Two-way Linked List
• In a singly linked list we can move from the header node to any
node in one direction only (left-right).
• A doubly linked list is a two-way list because one can move in either
direction. That is, either from left to right or from right to left.
• It contains a pointer to the next as well as to the previous node in
the sequence. Therefore, it consists of three parts—data, a pointer
to the next node, and a pointer to the previous node
PREVIOUS DATA NEXT
struct node
{
struct node *prev;
int data;
struct node *next;
};
Insertion on doubly linked list
Update the NEXT pointer of the new node to point to the current
head node and make PREV pointer of new node as NULL.
Header
9
1
2
Update head node’s left pointer to point to the new node and
make new node as head.
Header
4
1
9
2 3
Make a pointer variable PTR to point to the first node of the list.
Header Ptr
Move PTR so that it points to the last node of the list.
Header Ptr
Now, New node right pointer points to NULL and left pointer points
to the end of the list.
Header Ptr
2
9
1
Update right pointer of last node to point to new node.
Header Ptr
2
9
3 1
Make a pointer variable PTR to point to the first node of the list.
Header Ptr
Move PTR until node at (position-1) , after which the new node has to be
inserted.
Header Ptr
Insert the new node between PTR and the node succeeding it.
Header Ptr
1 2
9
Position node(node pointed by ptr) NEXT pointer points to the new node
and the next node of position node PREV pointer points to new node.
4
2 1
9
ptr NEXT
SET newnode
Insertion of a Node after a given node in DLL
Suppose we want to add a new node with data 9 after node with
data 15 in the list
Insert after node
New Node
Header with data 15 9
Given DLL
Make a pointer variable PTR to point to the first node of the list.
Header Ptr
Move PTR further until the data part of PTR = 15 after which the node has to be
inserted.
Header Ptr
Insert the new node between PTR and the node succeeding it.
Header Ptr
1 2
9
Position node(node pointed by ptr) NEXT pointer points to the new node
and the next node of position node PREV pointer points to new node.
Header Ptr (Position Node)
4
1 2
9
Make a pointer variable PTR to point to the first node of the list.
Header Ptr
Move PTR further until the data part of PTR = 7 before which the node has to be
inserted.
Header Ptr
Insert the new node between PTR and the node Preceding it.
Header Ptr
1 2
9
Position node(node pointed by ptr) PREV pointer points to the new node
and the previous node of position node’s NEXT pointer points to new
node.
Header Ptr (Position Node)
3 1
2
9
Create a temporary pointer Ptr to point to the same node as that of header.
Header Ptr
move the header to point to the next node and change the header’s PREV
pointer to NULL.
Ptr Header
Create a temporary pointer Ptr to point to the same node as that of header.
Header Ptr
Move Ptr so that it now points to the last node of the list.
Header Ptr
Store NULL in NEXT field of Ptr’s preceding node. And, Free the space
occupied by the node pointed by PTR
Header Ptr
dispose of the temporary node (pointed by Ptr).
Header Ptr
Header
Algorithm to delete the last node from DLL
Create a temporary pointer Ptr to point to the same node as that of header.
Header Ptr
Move PTR until node at (position-1) , after which the node has to be deleted.
Header
Ptr
TEMP
Update the previous node’s next pointer to the next node of the node to be
deleted. Next node’s PREV pointer to point to Previous node.
Header Ptr 2
TEMP
Dispose of the node Pointed by TEMP.
Header 2
Ptr
1
TEMP
Final DLL after deleting the node at Position-3
Header
Algorithm to delete a node at any position from DLL
Header
Given DLL
Create a temporary pointer Ptr to point to the same node as that of header.
Header Ptr
Move PTR further so that its data part is equal to the value after which the node
has to be deleted.
Header Ptr
TEMP
Update the previous node’s next pointer to the next node of the node to be
deleted. Next node’s PREV pointer to point to Previous node.
Header Ptr 2
TEMP
Dispose of the node Pointed by TEMP.
Header 2
Ptr
1
Final DLL
Header
Algorithm to delete a node after a given node from DLL
Header
Given DLL
Create a temporary pointer Ptr to point to the same node as that of header.
Header Ptr
Move PTR further till its data part is equal to the value before which the node
has to be deleted.
Header Ptr
TEMP
Update the previous node’s next pointer to the next node of the node to be
deleted. Next node’s PREV pointer to point to Previous node.
Header 2 Ptr
1
TEMP
Dispose of the node Pointed by TEMP.
Header 2 Ptr
1
Final DLL after deleting the node at Position-3
Header
Algorithm to delete a node before a given node from DLL
Forward Order : 4 15 7 40
Reverse Order : 40 7 15 4