A doubly linked list is a more complex data structure than a singly linked list, but it offers several advantages. The main advantage of a doubly linked list is that it allows for efficient traversal of the list in both directions. This is because each node in the list contains a pointer to the previous node and a pointer to the next node. This allows for quick and easy insertion and deletion of nodes from the list, as well as efficient traversal of the list in both directions.
A doubly linked list is a data structure that consists of a set of nodes, each of which contains a value and two pointers , one pointing to the previous node in the list and one pointing to the next node in the list. This allows for efficient traversal of the list in both directions , making it suitable for applications where frequent insertions and deletions are required.
Doubly Linked List
In a data structure, a doubly linked list is represented using nodes that have three fields:
Node Structure of Doubly Linked List
Here is how a node in a Doubly Linked List is typically represented:
Java
Python
JavaScript
Each node in a Doubly Linked List contains the data it holds, a pointer to the next node in the list, and a pointer to the previous node in the list. By linking these nodes together through the next and prev pointers, we can traverse the list in both directions (forward and backward), which is a key feature of a Doubly Linked List.
Let’s go through each of the operations mentioned above, one by one.
To Traverse the doubly list, we can use the following steps:
a. Forward Traversal:
b. Backward Traversal:
Below are the implementation of the above approach:
Java
Python
JavaScript
Forward Traversal: 1 2 3 Backward Traversal: 3 2 1
To find the length of doubly list, we can use the following steps:
Below are the implementation of the above approach:
Java
Python
JavaScript
Length of the doubly linked list: 3
Insertion at the Beginning in Doubly Linked List
To insert a new node at the beginning of the doubly list, we can use the following steps:
Below are the implementation of the above approach:
hardcoded linked list: C
Java
Python
C#
JavaScript
Original Linked List: 2 3 4 After inserting Node 1 at the front: 1 2 3 4
Insertion at the End in the Doubly Linked List
To insert a new node at the end of the doubly linked list, we can use the following steps:
Below are the implementation of the above approach:
<span If the linked list is empty, set the new C
<span If the linked list is empty, set the Java
<span If the linked list is empty, set the new node as Python
span If the linked list is empty, set the new node C#
<span If the linked list is empty, set the JavaScript
Original Linked List: 1 2 3 Inserting Node with data 4 at the end: 1 2 3 4
To insert a node at a specific Position in doubly linked list, we can use the following steps:
Insertion at a Specific Position in Doubly Linked List
To insert a new node at a specific position,
Below is the implementation of the above approach:
<span If the linked list is not empty, set the prev <span If the position is out of bounds <span If the new node is not the last node, update prev C
<span If the linked list is not empty, set the <span If the position is out of bounds <span If the new node is not the last node, update Java
<span If the linked list is not empty, set <span If the position is out of bounds <span If the new node is not the last node, update Python
span If the linked list is not empty, set the span If the position is out of bounds span If the new node is not the last node, update C#
<span If the position is out of bounds <span If the new node is not the last node, update JavaScript
Original Linked List: 1 2 4 Inserting Node with data 3 at position 3: 1 2 3 4
Deletion at the Beginning of Doubly Linked List
To delete a node at the beginning in doubly linked list, we can use the following steps:
Below is the implementation of the above approach:
C
Java
Python
C#
JavaScript
Original Linked List: 1 2 3 After Deletion at the beginning: 2 3
Deletion at the End in Doubly Linked List
To delete a node at the end in doubly linked list, we can use the following steps:
Below is the implementation of the above approach:
C
Java
Python
C#
JavaScript
Original Linked List: 1 2 3 After Deletion at the end: 1 2
Deletion at a Specific Position in Doubly Linked List
To delete a node at a specific position in doubly linked list, we can use the following steps:
Below is the implementation of the above approach:
<span If the list is empty <span If the position is out of range <span If the node to be deleted is the head node C
<span If the list is empty <span If the position is out of range <span If the node to be deleted is the head node Java
specific position in Doubly Linked List <span If the list is empty <span If the position is out of range <span If the node to be deleted is the head node Python
span If the list is empty span If the position is out of range span If the node to be deleted is the head node C#
<span If the list is empty <span If the position is out of range <span If the node to be deleted is the head node JavaScript
Original Linked List: 1 2 3 After Deletion at the position 2: 1 3