Doubly Linked List | Set 1 (Introduction and Insertion)
We strongly recommend to refer following post as a prerequisite of this post.
A Doubly Linked List (DLL) contains an extra pointer, typically called previous pointer, together with next pointer and data which are there in singly linked list.
Following is representation of a DLL node in C language.
/* Node of a doubly linked list */
structnode *next; // Pointer to next node in DLL
structnode *prev; // Pointer to previous node in DLL
Following are advantages/disadvantages of doubly linked list over singly linked list.
Advantages over singly linked list 1) A DLL can be traversed in both forward and backward direction. 2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given. 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) Every node of DLL Require extra space for an previous pointer. It is possible to implement DLL with single pointer though . 2) All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with next pointers. For example in following functions for insertions at different positions, we need 1 or 2 extra steps to set previous pointer.
Insertion A node can be added in four ways 1) At the front of the DLL 2) After a given node. 3) At the end of the DLL 4) Before a given node.
1) Add a node at the front: (A 5 steps process) The new node is always added before the head of the given Linked List. And newly added node becomes the new head of DLL. For example if the given Linked List is 10<->15<->20<->25 and we add an item 5 at the front, then the Linked List becomes 5<->10<->15<->20<->25. Let us call the function that adds at the front of the list is push(). The push() must receive a pointer to the head pointer, because push must change the head pointer to point to the new node (See this)
Following are the 5 steps to add node at the front.
/* Given a reference (pointer to pointer) to the head of a list
and an int, inserts a new node on the front of the list. */
/* 4. Make next of new node as next of prev_node */
new_node->next = prev_node->next;
/* 5. Make the next of prev_node as new_node */
prev_node->next = new_node;
/* 6. Make prev_node as previous of new_node */
new_node->prev = prev_node;
/* 7. Change previous of new_node's next node */
if(new_node->next != NULL)
new_node->next->prev = new_node;
Five of the above steps step process are same as the 5 steps used for inserting after a given node in singly linked list. The two extra steps are needed to change previous pointer of new node and previous pointer of new node’s next node.
3) Add a node at the end: (7 steps process) The new node is always added after the last node of the given Linked List. For example if the given DLL is 5<->10<->15<->20<->25 and we add an item 30 at the end, then the DLL becomes 5<->10<->15<->20<->25<->30. Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node.
Following are the 7 steps to add node at the end.
/* Given a reference (pointer to pointer) to the head
of a DLL and an int, appends a new node at the end */