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

Doubly Linked List

Uploaded by

nvkeerthuvcet
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Doubly Linked List

Uploaded by

nvkeerthuvcet
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

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.

• In singly linked list, to delete a node, pointer to the previous node is


needed. To get this previous node, sometimes the list is traversed. In
DLL, we can get the previous node using previous pointer.
Disadvantages over singly
linked list
1. Memory Overhead: Every node of DLL Require extra space for an
previous pointer.
2. Pointer manipulation: All operations require an extra
pointer previous to be maintained. For example, in insertion, we
need to modify previous pointers together with next pointers.
3. Slower Access time: Functions for insertions at different positions,
we need 1 or 2 extra steps to set previous pointer.
Applications of DLL
• Implementing a Hash Table: Doubly linked lists can
be used to implement hash tables, which are used to
store and retrieve data efficiently based on a key.
• Dynamic Memory Allocation: In systems
programming, doubly linked lists can be used to
manage dynamic memory allocation, where memory
blocks are allocated and deallocated as needed.
• Reversing a List: A doubly linked list can be used to
reverse a list efficiently by swapping the previous and
next pointers of each node.
Operations
• Insertion
• Deletion
• Searching
• Display
Insertion
• At the front
• After a given node.
• At the end
At the front
• Allocate memory for a new node (say new_node) and
assign the provided value to its data field.
• Set the previous pointer of the new_node to null.
• If the list is empty:
• Set the next pointer of the new_node to null.
• Update the head pointer to point to the new_node.
• If the list is not empty:
• Set the next pointer of the new_node to the current head.
• Update the previous pointer of the current head to point to
the new_node.
• Update the head pointer to point to the new_node
At the front of the DLL

•Note: Auxiliary space: total space taken by


algorithm with respect to input size
Insertion in between two nodes in Doubly
Linked List
Add a node after a given node in a Doubly
Linked List

• Firstly create a new node (say new_node).


• Now insert the data in the new node.
• Point the next of new_node to the next of prev_node.
• Point the next of prev_node to new_node.
• Point the previous of new_node to prev_node.
• Point the previous of next of new_node to new_node.
Add a node before a given node in a Doubly
Linked List:

• Allocate memory for the new node, let it be


called new_node.
• Put the data in new_node.
• Set the previous pointer of this new_node as the
previous node of the next_node.
• Set the previous pointer of the next_node as
the new_node.
• Set the next pointer of this new_node as
the next_node.
• Set the next pointer of the previous
of new_node to new_node.
Insertion at the End in Doubly Linked List:

• 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.

• Consider this DLL


Categories in Deletion:
• After the deletion of the head 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:

You might also like