Doubly Linked List
Doubly Linked List
Doubly linked list is a complex type of linked list in which a node contains a
pointer to the previous as well as the next node in the sequence. Therefore, in a
doubly linked list, a node consists of three parts: node data, pointer to the next
node in sequence (next pointer) , pointer to the previous node (previous pointer). A
sample node in a doubly linked list is shown in the figure.
A doubly linked list containing three nodes having numbers from 1 to 3 in their
data part, is shown in the following image.
struct node
{
struct node *prev;
int data;
struct node *next;
}
The prev part of the first node and the next part of the last node will always
contain null indicating end in each direction.
In a singly linked list, we could traverse only in one direction, because each node
contains address of the next node and it doesn't have any record of its previous
nodes. However, doubly linked list overcome this limitation of singly linked list.
Due to the fact that, each node of the list contains the address of its previous node,
we can find all the details about the previous node as well by using the previous
address stored inside the previous part of each node.
1. Insertion
2. Deletion
3. Display
Insertion
In a double linked list, the insertion operation can be performed in three ways as follows...
● Step 1 - Create a newNode with given value and newNode → previous as NULL.
● Step 2 - Check whether list is Empty (head == NULL)
● Step 3 - If it is Empty then, assign NULL to newNode → next and newNode to head.
● Step 4 - If it is not Empty then, assign head to newNode → next and newNode to
head.
Inserting At End of the list
We can use the following steps to insert a new node at the end of the doubly linked list.
● Step 1 - Create a newNode with given value and newNode → next as NULL.
● Step 2 - Check whether list is Empty (head == NULL)
● Step 3 - If it is Empty, then assign NULL to newNode → previous and
newNode to head.
● Step 4 - If it is not Empty, then, define a node pointer temp and initialize with
head.
● Step 5 - Keep moving the temp to its next node until it reaches to the last node
in the list (until temp → next is equal to NULL).
● Step 6 - Assign newNode to temp → next and temp to newNode → previous.
Deletion
In a double linked list, the deletion operation can be performed in three ways as follows...
There are various applications of doubly linked list in the real world. Some of them
can be listed as:
● Doubly linked list can be used in navigation systems where both front and
back navigation is required.
● It is used by browsers to implement backward and forward navigation of
visited web pages i.e. back and forward buttons.
● It is also used by various applications to implement Undo and Redo
functionality.
● It can also be used to represent a deck of cards in games.
● It is also used to represent various states of a game.