Doubly linked list operations
We will explore the following operations in our doubly linked list implementation. Though they sound similar to those used in singly linked lists, they have a major difference in their implementations:
- Inserting at the first node
- Inserting at the last node
- Inserting before a specific node
- Inserting after a specific node
- Deleting the first node
- Deleting the last node
- Searching for and deleting one node
- Displaying the list forward
- Displaying the list backward
Inserting at first the node
When we add a node at the front or head, we have to check whether the list is empty or not. If the list is empty, both the first and last node will point to the newly created node. However, if the list already has a head, then we have to do the following:
- Create the new node.
- Make the new node as the first node or head.
- Assign the previous head or first node as the next, to follow the newly created first node.
- Assign the previous first node's previous link to the new first node.
Here is the code...