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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Sort the biotonic doubly linked list | Set-2
- Python | Stack using Doubly Linked List
- Reverse a doubly circular linked list
- Python | Queue using Doubly Linked List
- Implementation of Deque using doubly linked list
- Sort a k sorted doubly linked list
- Delete all the even nodes from a Doubly Linked List
- Bubble Sort On Doubly Linked List
- Delete all occurrences of a given key in a doubly linked list
- Sort the biotonic doubly linked list
- Reverse a Doubly linked list using recursion
- Merge Sort for Doubly Linked List
- Find pairs with given sum in 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.