Doubly Linked List Implementation
Doubly Linked List (DLL) is a complex data structure and an advanced version of a simple linked list in which the node has a pointer to the next node only. As we can traverse the elements in only one direction, reverse traversing is not possible. To solve this problem, a doubly-linked list came into the picture as each node contains the address of the previous and next node, so both the forward and backward traversing of the list is possible. So every node in a doubly-linked list contains 3 parts, i.e., the node which stores the actual item and the other parts contain the pointers containing the address of the previous and next node in the sequence. In this topic, we are going to learn about the Doubly linked list in C.
A Doubly Linked List is a unique type of Data Structure where there are a chain of nodes, that are connected to one another using pointers, where any individual node has 3 components -
- Data - *prev
- Previous Pointer - data
- Next Pointer - *next
For any node, its previous pointer contains the address of the previous node and the next pointer contains the address of the next node in the chain of nodes.
Following is a Doubly Linked List representation.
Algorithm
Operations on Doubly Linked List
Node creation :
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head;
Other Operations :
- Insertion at beginning - Adding the node into the linked list at beginning.
- Insertion at end - Adding the node into the linked list to the end.
- Insertion after specified node - Adding the node into the linked list after the specified node.
- Deletion at beginning - Removing the node from beginning of the list
- Deletion at the end - Removing the node from end of the list.
- Deletion of the node having given data - Removing the node which is present just after the node containing the given data.
- Searching - Comparing each node data with the item to be searched and return the location of the item in the list if the item found else return null.
- Traversing - Visiting each node of the list at least once in order to perform some specific operation like searching, sorting, display, etc.