Doubly Linked List
Doubly Linked List
(DLL)
Introduction
• Doubly Linked List is a variation of Linked list in which navigation is
possible in both ways, either forward and backward easily as
compared to Single Linked List.
• Following are the important terms to understand the concept of
doubly linked list.
• Link − Each link of a linked list can store a data called an element.
• Next − Each link of a linked list contains a link to the next link called
Next.
• Prev − Each link of a linked list contains a link to the previous link
called Prev.
Doubly Linked List
Representation
• Each node carries a data field and two link fields called next and prev.
• Each node is linked with its next node using its next pointer value.
• Each node is linked with its previous node using its previous pointer
value.
• The last node carries a link as null to mark the end of the list.
• Doubly linked list can traverse in both the direction.
Characteristics of DLL
• Dynamic size: The size of a doubly linked list can
change dynamically, meaning that nodes can be added
or removed as needed.
• Two-way navigation: In a doubly linked list, each node
contains pointers to both the previous and next
elements, allowing for navigation in both forward and
backward directions.
• Memory overhead: Each node in a doubly linked list
requires memory for two pointers (previous and next),
in addition to the memory required for the data stored
in the node.
Advantages over singly linked
list
1. Two-way navigation: A DLL can be traversed in both forward
and backward direction.
2. Efficient insertion and deletion: The delete operation in
DLL is more efficient if pointer to the node to be deleted is given.
3.Versatility: We can quickly insert a new node before a given
node.
• The new node is always added after the last node of the given
Linked List.
• Steps:
• Create a new node (say new_node).
• Put the value in the new node.
• Make the next pointer of new_node as null.
• If the list is empty, make new_node as the head.
• Otherwise, travel to the end of the linked list.
• Now make the next pointer of last node point to new_node.
• Change the previous pointer of new_node to the last node of the
list.
Insert at end
Deletion
1.If the node to be deleted is the head node then make
the next node as head.
2.If a node is deleted, connect the next and previous
node of the deleted node.
• Output:
• After the deletion of the middle node.
• Output:
• After the deletion of the last node.
• Output:
Searching
• Iterate over the linked list and for every node, check if
data is found, return true
• Otherwise, return false
Searching
Display
Driver code:
Output: