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.

Doubly Linked List | Set 1 (Introduction and Insertion)

Following is representation of a DLL node in C language.

[pastacode lang=”cpp” manual=”%2F*%20Node%20of%20a%20doubly%20linked%20list%20*%2F%0Astruct%20node%0A%7B%0A%20%20int%20data%3B%0A%20%20struct%20node%20*next%3B%20%2F%2F%20Pointer%20to%20next%20node%20in%20DLL%0A%20%20struct%20node%20*prev%3B%20%2F%2F%20Pointer%20to%20previous%20node%20in%20DLL%0A%7D%3B” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

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 (See this and this).
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.

[ad type=”banner”]

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 10152025 and we add an item 5 at the front, then the Linked List becomes 510152025. 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)

Doubly Linked List | Set 1 (Introduction and Insertion)

Following are the 5 steps to add node at the front.

[pastacode lang=”cpp” manual=”%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%20of%20a%20list%0A%20%20%20and%20an%20int%2C%20inserts%20a%20new%20node%20on%20the%20front%20of%20the%20list.%20*%2F%0Avoid%20push(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%201.%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%202.%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%203.%20Make%20next%20of%20new%20node%20as%20head%20and%20previous%20as%20NULL%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20(*head_ref)%3B%0A%20%20%20%20new_node-%3Eprev%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%204.%20change%20prev%20of%20head%20node%20to%20new%20node%20*%2F%0A%20%20%20%20if((*head_ref)%20!%3D%20%20NULL)%0A%20%20%20%20%20%20(*head_ref)-%3Eprev%20%3D%20new_node%20%3B%0A%20%0A%20%20%20%20%2F*%205.%20move%20the%20head%20to%20point%20to%20the%20new%20node%20*%2F%0A%20%20%20%20(*head_ref)%20%20%20%20%3D%20new_node%3B%0A%7D” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Four steps of the above five steps are same as the 4 steps used for inserting at the front in singly linked list. The only extra step is to change previous of head.

2) Add a node after a given node.: (A 7 steps process)
We are given pointer to a node as prev_node, and the new node is inserted after the given node.

[pastacode lang=”cpp” manual=”%2F*%20Given%20a%20node%20as%20prev_node%2C%20insert%20a%20new%20node%20after%20the%20given%20node%20*%2F%0Avoid%20insertAfter(struct%20node*%20prev_node%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*1.%20check%20if%20the%20given%20prev_node%20is%20NULL%20*%2F%0A%20%20%20%20if%20(prev_node%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20printf(%22the%20given%20previous%20node%20cannot%20be%20NULL%22)%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%202.%20allocate%20new%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20%2F*%203.%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%204.%20Make%20next%20of%20new%20node%20as%20next%20of%20prev_node%20*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20prev_node-%3Enext%3B%0A%20%0A%20%20%20%20%2F*%205.%20Make%20the%20next%20of%20prev_node%20as%20new_node%20*%2F%0A%20%20%20%20prev_node-%3Enext%20%3D%20new_node%3B%0A%20%0A%20%20%20%20%2F*%206.%20Make%20prev_node%20as%20previous%20of%20new_node%20*%2F%0A%20%20%20%20new_node-%3Eprev%20%3D%20prev_node%3B%0A%20%0A%20%20%20%20%2F*%207.%20Change%20previous%20of%20new_node’s%20next%20node%20*%2F%0A%20%20%20%20if%20(new_node-%3Enext%20!%3D%20NULL)%0A%20%20%20%20%20%20new_node-%3Enext-%3Eprev%20%3D%20new_node%3B%0A%7D” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

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 510152025 and we add an item 30 at the end, then the DLL becomes 51015202530.
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.

Doubly Linked List | Set 1 (Introduction and Insertion)

Following are the 7 steps to add node at the end

[pastacode lang=”cpp” manual=”%2F*%20Given%20a%20reference%20(pointer%20to%20pointer)%20to%20the%20head%0A%20%20%20of%20a%20DLL%20and%20an%20int%2C%20appends%20a%20new%20node%20at%20the%20end%20%20*%2F%0Avoid%20append(struct%20node**%20head_ref%2C%20int%20new_data)%0A%7B%0A%20%20%20%20%2F*%201.%20allocate%20node%20*%2F%0A%20%20%20%20struct%20node*%20new_node%20%3D%20(struct%20node*)%20malloc(sizeof(struct%20node))%3B%0A%20%0A%20%20%20%20struct%20node%20*last%20%3D%20*head_ref%3B%20%20%2F*%20used%20in%20step%205*%2F%0A%20%0A%20%20%20%20%2F*%202.%20put%20in%20the%20data%20%20*%2F%0A%20%20%20%20new_node-%3Edata%20%20%3D%20new_data%3B%0A%20%0A%20%20%20%20%2F*%203.%20This%20new%20node%20is%20going%20to%20be%20the%20last%20node%2C%20so%0A%20%20%20%20%20%20%20%20%20%20make%20next%20of%20it%20as%20NULL*%2F%0A%20%20%20%20new_node-%3Enext%20%3D%20NULL%3B%0A%20%0A%20%20%20%20%2F*%204.%20If%20the%20Linked%20List%20is%20empty%2C%20then%20make%20the%20new%0A%20%20%20%20%20%20%20%20%20%20node%20as%20head%20*%2F%0A%20%20%20%20if%20(*head_ref%20%3D%3D%20NULL)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20new_node-%3Eprev%20%3D%20NULL%3B%0A%20%20%20%20%20%20%20%20*head_ref%20%3D%20new_node%3B%0A%20%20%20%20%20%20%20%20return%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F*%205.%20Else%20traverse%20till%20the%20last%20node%20*%2F%0A%20%20%20%20while%20(last-%3Enext%20!%3D%20NULL)%0A%20%20%20%20%20%20%20%20last%20%3D%20last-%3Enext%3B%0A%20%0A%20%20%20%20%2F*%206.%20Change%20the%20next%20of%20last%20node%20*%2F%0A%20%20%20%20last-%3Enext%20%3D%20new_node%3B%0A%20%0A%20%20%20%20%2F*%207.%20Make%20last%20node%20as%20previous%20of%20new%20node%20*%2F%0A%20%20%20%20new_node-%3Eprev%20%3D%20last%3B%0A%20%0A%20%20%20%20return%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

 

Six of the above 7 steps are same as the 6 steps used for inserting after a given node in singly linked list. The one extra step is needed to change previous pointer of new node.

4) Add a node before a given node
This is left as an exercise for the readers.

A complete working program to test above functions.
Following is complete C program to test above functions.

Python Programming:

[pastacode lang=”python” manual=”%23%20A%20complete%20working%20Python%20program%20to%20demonstrate%20all%0A%23%20insertion%20methods%0A%20%0A%23%20A%20linked%20list%20node%0Aclass%20Node%3A%0A%20%0A%20%20%20%20%23%20Constructor%20to%20create%20a%20new%20node%0A%20%20%20%20def%20__init__(self%2C%20data)%3A%0A%20%20%20%20%20%20%20%20self.data%20%3D%20data%0A%20%20%20%20%20%20%20%20self.next%20%3D%20None%0A%20%20%20%20%20%20%20%20self.prev%20%3D%20None%0A%20%0A%23%20Class%20to%20create%20a%20Doubly%20Linked%20List%0Aclass%20DoublyLinkedList%3A%0A%20%0A%20%20%20%20%23%20Constructor%20for%20empty%20Doubly%20Linked%20List%0A%20%20%20%20def%20__init__(self)%3A%0A%20%20%20%20%20%20%20%20self.head%20%3D%20None%0A%20%0A%20%20%20%20%23%20Given%20a%20reference%20to%20the%20head%20of%20a%20list%20and%20an%0A%20%20%20%20%23%20integer%2Cinserts%20a%20new%20node%20on%20the%20front%20of%20list%0A%20%20%20%20def%20push(self%2C%20new_data)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%201.%20Allocates%20node%0A%20%20%20%20%20%20%20%20%23%202.%20Put%20the%20data%20in%20it%0A%20%20%20%20%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%0A%20%20%20%20%20%20%20%20%23%203.%20Make%20next%20of%20new%20node%20as%20head%20and%0A%20%20%20%20%20%20%20%20%23%20previous%20as%20None%20(already%20None)%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20self.head%0A%20%0A%20%20%20%20%20%20%20%20%23%204.%20change%20prev%20of%20head%20node%20to%20new_node%0A%20%20%20%20%20%20%20%20if%20self.head%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20self.head.prev%20%3D%20new_node%0A%20%0A%20%20%20%20%20%20%20%20%23%205.%20move%20the%20head%20to%20point%20to%20the%20new%20node%0A%20%20%20%20%20%20%20%20self.head%20%3D%20new_node%0A%20%0A%20%20%20%20%23%20Given%20a%20node%20as%20prev_node%2C%20insert%20a%20new%20node%20after%0A%20%20%20%20%23%20the%20given%20node%0A%20%20%20%20def%20insertAfter(self%2C%20prev_node%2C%20new_data)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%201.%20Check%20if%20the%20given%20prev_node%20is%20None%0A%20%20%20%20%20%20%20%20if%20prev_node%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20%22the%20given%20previous%20node%20cannot%20be%20NULL%22%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%20%20%20%20%20%23%202.%20allocate%20new%20node%0A%20%20%20%20%20%20%20%20%23%203.%20put%20in%20the%20data%0A%20%20%20%20%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%0A%20%20%20%20%20%20%20%20%23%204.%20Make%20net%20of%20new%20node%20as%20next%20of%20prev%20node%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20prev_node.next%0A%20%0A%20%20%20%20%20%20%20%20%23%205.%20Make%20prev_node%20as%20previous%20of%20new_node%0A%20%20%20%20%20%20%20%20prev_node.next%20%3D%20new_node%0A%20%0A%20%20%20%20%20%20%20%20%23%206.%20Make%20prev_node%20ass%20previous%20of%20new_node%0A%20%20%20%20%20%20%20%20new_node.prev%20%3D%20prev_node%0A%20%0A%20%20%20%20%20%20%20%20%23%207.%20Change%20previous%20of%20new_nodes’s%20next%20node%0A%20%20%20%20%20%20%20%20if%20new_node.next%20is%20not%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20new_node.next.prev%20%3D%20new_node%0A%20%0A%20%20%20%20%23%20Given%20a%20reference%20to%20the%20head%20of%20DLL%20and%20integer%2C%0A%20%20%20%20%23%20appends%20a%20new%20node%20at%20the%20end%0A%20%20%20%20def%20append(self%2C%20new_data)%3A%0A%20%0A%20%20%20%20%20%20%20%20%23%201.%20Allocates%20node%0A%20%20%20%20%20%20%20%20%23%202.%20Put%20in%20the%20data%0A%20%20%20%20%20%20%20%20new_node%20%3D%20Node(new_data)%0A%20%0A%20%20%20%20%20%20%20%20%23%203.%20This%20new%20node%20is%20going%20to%20be%20the%20last%20node%2C%0A%20%20%20%20%20%20%20%20%23%20so%20make%20next%20of%20it%20as%20None%0A%20%20%20%20%20%20%20%20new_node.next%20%3D%20None%0A%20%0A%20%20%20%20%20%20%20%20%23%204.%20If%20the%20Linked%20List%20is%20empty%2C%20then%20make%20the%0A%20%20%20%20%20%20%20%20%23%20new%20node%20as%20head%0A%20%20%20%20%20%20%20%20if%20self.head%20is%20None%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20new_node.prev%20%3D%20None%0A%20%20%20%20%20%20%20%20%20%20%20%20self.head%20%3D%20new_node%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%20%20%20%20%20%23%205.%20Else%20traverse%20till%20the%20last%20node%0A%20%20%20%20%20%20%20%20last%20%3D%20self.head%0A%20%20%20%20%20%20%20%20while(last.next%20is%20not%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20last%20%3D%20last.next%0A%20%0A%20%20%20%20%20%20%20%20%23%206.%20Change%20the%20next%20of%20last%20node%0A%20%20%20%20%20%20%20%20last.next%20%3D%20new_node%0A%20%0A%20%20%20%20%20%20%20%20%23%207.%20Make%20last%20node%20as%20previous%20of%20new%20node%0A%20%20%20%20%20%20%20%20new_node.prev%20%3D%20last%0A%20%0A%20%20%20%20%20%20%20%20return%0A%20%0A%20%20%20%20%23%20This%20function%20prints%20contents%20of%20linked%20list%0A%20%20%20%20%23%20starting%20from%20the%20given%20node%0A%20%20%20%20def%20printList(self%2C%20node)%3A%0A%20%0A%20%20%20%20%20%20%20%20print%20%22%5CnTraversal%20in%20forward%20direction%22%0A%20%20%20%20%20%20%20%20while(node%20is%20not%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20%22%20%25d%22%20%25(node.data)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20last%20%3D%20node%0A%20%20%20%20%20%20%20%20%20%20%20%20node%20%3D%20node.next%0A%20%0A%20%20%20%20%20%20%20%20print%20%22%5CnTraversal%20in%20reverse%20direction%22%0A%20%20%20%20%20%20%20%20while(last%20is%20not%20None)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20%22%20%25d%22%20%25(last.data)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20last%20%3D%20last.prev%0A%20%0A%23%20Driver%20program%20to%20test%20above%20functions%0A%20%0A%23%20Start%20with%20empty%20list%0Allist%20%3D%20DoublyLinkedList()%0A%20%0A%23%20Insert%206.%20So%20the%20list%20becomes%206-%3ENone%0Allist.append(6)%0A%20%0A%23%20Insert%207%20at%20the%20beginning.%0A%23%20So%20linked%20list%20becomes%207-%3E6-%3ENone%0Allist.push(7)%0A%20%0A%23%20Insert%201%20at%20the%20beginning.%0A%23%20So%20linked%20list%20becomes%201-%3E7-%3E6-%3ENone%0Allist.push(1)%0A%20%0A%23%20Insert%204%20at%20the%20end.%0A%23%20So%20linked%20list%20becomes%201-%3E7-%3E6-%3E4-%3ENone%0Allist.append(4)%0A%20%0A%23%20Insert%208%2C%20after%207.%0A%23%20So%20linked%20list%20becomes%201-%3E7-%3E8-%3E6-%3E4-%3ENone%0Allist.insertAfter(llist.head.next%2C%208)%0A%20%0Aprint%20%22Created%20DLL%20is%3A%20%22%2C%0Allist.printList(llist.head)%0A%20%0A” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

Output:

 Created DLL is:
Traversal in forward direction
 1  7  8  6  4
Traversal in reverse direction
 4  6  8  7  1