0% found this document useful (0 votes)
92 views

5 Double Linked List and Its Operations

The document discusses the structure and operations of doubly linked lists. A doubly linked list contains pointers to both the next and previous nodes, allowing traversal in both directions. The basic operations covered are insertion, deletion, traversal, and search. Insertion and deletion can occur at the beginning, end, or middle of the list. Algorithms for each operation are provided.

Uploaded by

Mohammad Junaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

5 Double Linked List and Its Operations

The document discusses the structure and operations of doubly linked lists. A doubly linked list contains pointers to both the next and previous nodes, allowing traversal in both directions. The basic operations covered are insertion, deletion, traversal, and search. Insertion and deletion can occur at the beginning, end, or middle of the list. Algorithms for each operation are provided.

Uploaded by

Mohammad Junaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Data Structures

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

Structure of the node in Double Linked List


• Where, DATA field  stores the element or data,
PREVIOUS field  contains the address of its previous node,
NEXT field  contains the address of its next node.
Header Representation of Double Linked List

The PREV field


of the first
node and
NEXT field the
of
-1NULL
the last node
will contain
NULL..

Memory Representation of a Doubly Linked list


Basic Operations on Doubly Linked List
• Insertion
• Deletion
• Traverse
• Search
The node structure of a doubly linked list in C,

struct node
{
struct node *prev;
int data;
struct node *next;
};
Insertion on doubly linked list

•There are 3 different cases of Insertion into a doubly-


linked list:

 Inserting a new node at the front.


 Inserting a new node at the end of the list.
 Inserting a new node at the middle of
the list(at any position)
Extra cases:
 Inserting a new node before given
node
Inserting a new node after given node
Insertion of a Node at the Front of DLL
Suppose we want to add a new node with data 9 as the
first node of the list New Node
Header 9
Given DLL

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

Algorithm to insert a new node at the beginning of DLL


Step 1: SET newnodedata = value
Step 2: SET newnode PREV = NULL
Step 3: SET newnode NEXT= header
Step 4: SET header  PREV =
Step 5: newnode SET
Step 6: header=newnode
EXIT
Insertion of a Node at the End of DLL
Suppose we want to add a new node with data 9 as the
last node of the list New Node
Header 9
Given DLL

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

Algorithm to insert a new node at the END of DLL


Step 1: SET newnodedata = value
Step 2: SET newnode NEXT = NULL
Step 3: SET ptr = header
Step 4: Repeat Step-5 while ptr NEXT !=NULL
Step 5: SET ptr = ptrNEXT
[END OF LOOP]
Step 6: SET ptr NEXT= newnode
Step 7: SET newnode  PREV =
Step 8: ptr
EXIT
Insertion of a Node at any position in DLL
Suppose we want to add a new node with data 9 at as a 3rd node in
the list Insert position=3 New Node
Header 9
Given DLL

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.

Header Ptr (Position Node)

4
2 1
9

Header Final DLL after Insertion


Algorithm to insert a new node at any position in the DLL

Step 1: SET newnodedata = value


Step 2: SET ptr = header
Step 3: SET currentPosition = 1
Step 4: Repeat Step-5 while
Step 5: currentPosition<(givenPositi
on-1)
SET ptr =
Step 6: ptrNEXT
Step 7: currentPosition+
Step 8: + [END OF LOOP]
Step 9: SET newnode
Step 10: NEXT =

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

Header Final DLL after Insertion


Algorithm to insert a new node after a given node in the DLL

Step 1: SET newnodedata = value


Step 2: SET ptr = header
Step 3: Repeat Step-4 while ptr  DATA != NUMBER
Step 4: SETptr = ptrNEXT
[END OF LOOP]
Step 5: SET newnode NEXT = ptr
Step 6: NEXT SET newnode  PREV = ptr
Step 7: SET ptr NEXT  PREV = newnode
Step8: SET ptr NEXT =
Step newnode EXIT
9:
Insertion of a Node Before a given node in DLL
Suppose we want to add a new node with data 9 before node with
data 7 in the list
Insert before node
New Node
Header with data 7 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 = 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

Header Final DLL after Insertion


Algorithm to insert a new node before a given node in the
DLL
Step 1: SET newnodedata = value
Step 2: SET ptr = header
Step 3: Repeat Step-4 while ptr  DATA != NUMBER
Step 4: SET ptr = ptrNEXT
[END OF LOOP]
Step 5: SET newnode NEXT = ptr
Step 6: SET newnode  PREV = ptr  PREV
Step 7: SET ptr  PREV  NEXT = newnode
Step8: SET ptr PREV = newnode
Step EXIT
9:
Deletion on doubly linked list

•There are 3 different cases of Deletion into a doubly-


linked list:

 Deleting a node at the front.


 Deleting a node at the end of the list.
 Deleting a node at the middle of
the list(at any position)
Extra cases:
 Deleting a node before given
node
Deleting a node after given node
Deleting a node at the Front of DLL
Suppose we want to delete a node with data 4 as the first
node of the list Data to be deleted= 4
Header
Given DLL

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

dispose of the temporary node (pointed by Ptr).


Ptr Header
Final DLL after deleting the front node
Header

Algorithm to delete the first node from DLL


Step 1: IF Header = NULL
print
“List is empty”
Go to Step 6
[END OF IF]
Step 2: SET Ptr =
Header
Step 3: SET Header = Header
NEXT Step 4: SET Header
Deleting a Last node from DLL
Suppose we want to delete a node with data 40 as the last node
of the list Data to be deleted= 40
Header
Given DLL

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

Final DLL after deleting the Last node

Header
Algorithm to delete the last node from DLL

Step 1: IF Header = NULL


print “List is
empty”
Go to Step 7
[END OF IF]
Step 2: SET Ptr =
Header
Step 4: SET Ptr = Ptr NEXT
[END OFRepeat
Step 3: LOOP]
Step-4
Step 5: while Ptr
SET Ptr PREV
NEXT ! NEXT =
= NULLStep 6: FREE PTR
NULL
Step 7: EXIT
Deleting an Intermediate node from DLL
Suppose we want to delete a node with data 7 at position 3 from
the list Node to be deleted at position= 3
Header
Given 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

Create TEMP temporary Pointer to point to the node 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

Step 1: IF Header = NULL


print “List is empty”
Go to Step 10
[END OF IF]
Step 2: SET Ptr = Header
Step 3: SET currentPosition = 1
Step 4: Repeat Step-5 while currentPosition<(givenPosition-1)
Step 5: SET Ptr = Ptr NEXT
currentPosition ++
[END OF LOOP]
Step 6: SET Temp = PtrNEXT
Step 7: SET PtrNEXT = Temp  NEXT
Step 8: SET TempNEXT PREV = Ptr
Step 9: FREE Temp
Step 10: EXIT
Deleting a node after a given node in DLL
Delete a node after node with data 15

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

Create TEMP temporary Pointer to point to the node 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

Step 1: IF Header = NULL


print “List is
empty”
Go to Step 9
[END OF IF]
Step 2: SET Ptr = Header
Step 3:
4: RepeatSETStep-4
Ptr =while
PtrPtrDATA
NEXT != value
[END OF LOOP]
Step 5: SET Temp =
PtrNEXT
Step 6: SET PtrNEXT = Temp 
NEXT Step 7: SET TempNEXT PREV
= Ptr Step 8: FREE Temp
Step 9: EXIT
Deleting a node before a given node in DLL
Delete a node before the node with data 40

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

Create TEMP temporary Pointer to point to the node 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

Step 1: IF Header = NULL


print “List is
empty”
Go to Step 10
[END OF IF]
Step 2: SET Ptr = Header
Step 3:
4: RepeatSETStep-4
Ptr =while
PtrPtrDATA
NEXT != value
[END OF LOOP]
Step 5: SET Temp =
PtrPREV
Step 6: SET Temp 
PREVNEXT = Ptr Step 7: SET Ptr
PREV = Temp PREV Step 8: FREE
Temp
Step 9: EXIT
Traversing
• Traversing a linked DLL
list means accessing all the nodes of the list
in order to perform some processing on them.
• Example: Displaying the contents of Linked list,
Counting
number of nodes in the linked list, etc..

Algorithm for Displaying Double Linked List:


Step -1: ptr = header
Step-2:
Step-3: Repeat Steps-3
print and 4 while
ptrptr != NULL
Step-  data Set ptr =
4: ptr next
Step 5: Stop
[END OF LOOP]
Displaying DLL in reverse Order
Algorithm for Displaying Double Linked List in reverse order:
Step -1: ptr = header
Step-2: Repeat Steps-3 while ptr NEXT != NULL
Step-3: Set ptr = ptr next
[END OF LOOP]
Step-4: Repeat Steps-5 and
6 while ptr
Step-5: print ptr
Step-  data Set ptr =
!=
6: ptr PREV
Step 5: Stop
[END OF LOOP]
NULL
Ptr Given DLL
Header

Forward Order : 4 15 7 40

Reverse Order : 40 7 15 4

You might also like