A Doubly Linked List (DLL) contains an extra pointer, typically called the previous pointer, together with the next pointer and data which are there in a singly linked list.
Below are operations on the given DLL:
- Add a node at the front of DLL: The new node is always added before the head of the given Linked List. And the newly added node becomes the new head of DLL & maintaining a global variable for counting the total number of nodes at that time.
- Traversal of a Doubly linked list
- Insertion of a node: This can be done in three ways:
- At the beginning: The new created node is insert in before the head node and head points to the new node.
- At the end: The new created node is insert at the end of the list and tail points to the new node.
- At a given position: Traverse the given DLL to that position(let the node be X) then do the following:
- Change the next pointer of new Node to the next pointer of Node X.
- Change the prev pointer of next Node of Node X to the new Node.
- Change the next pointer of node X to new Node.
- Change the prev pointer of new Node to the Node X.
- Deletion of a node: This can be done in three ways:
- At the beginning: Move head to the next node to delete the node at the beginning and make previous pointer of current head to NULL .
- At the last: Move tail to the previous node to delete the node at the end and make next pointer of tail node to NULL.
- At a given position: Let the prev node of Node at position pos be Node X and next node be Node Y, then do the following:
- Change the next pointer of Node X to Node Y.
- Change the previous pointer of Node Y to Node X.
Below is the implementation of all the operations:
Linked List: 2 4 9 1 21 22
Linked List after inserting 1 at beginning: 1 2 4 9 1 21 22
Linked List after inserting 0 at end: 1 2 4 9 1 21 22 0
Linked List after inserting 44 after 3rd Node: 1 2 4 44 9 1 21 22 0
Linked List after deleting node at beginning: 2 4 44 9 1 21 22 0
Linked List after deleting node at end: 2 4 44 9 1 21 22
Linked List after deleting node at position 5: 2 4 44 9 21 22
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Difference between Singly linked list and Doubly linked list
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Construct a Doubly linked linked list from 2D Matrix
- Reverse a Doubly Linked List | Set-2
- QuickSort on Doubly Linked List
- Reverse a Doubly Linked List
- Reverse a doubly circular linked list
- Priority Queue using doubly linked list
- Delete all the even nodes from a Doubly Linked List
- Python | Stack using Doubly Linked List
- Bubble Sort On Doubly Linked List
- Python | Queue using Doubly Linked List
- Sort the biotonic doubly linked list | Set-2
- Merge Sort for Doubly Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.