Doubly Linked List
Doubly Linked List
Linked List (DLL)
Following are advantages/disadvantages of doubly linked list 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.
3) We can quickly insert a new node before a given node.
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.
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.
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)
We are given pointer to a node as prev_node, and the new node is inserted after the given node
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.
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:
steps:
Let the pointer to this given node be next_node and the data of the new node to be
added as new_data.
1. Check if the next_node is NULL or not. If it’s NULL, return from the function because any new node can
not be added before a NULL
2. Allocate memory for the new node, let it be called new_node
3. Set new_node->data = new_data
4. Set the previous pointer of this new_node as the previous node of the next_node, new_node->prev =
next_node->prev
5. Set the previous pointer of the next_node as the new_node, next_node->prev = new_node
6. Set the next pointer of this new_node as the next_node, new_node->next = next_node;
7. If the previous node of the new_node is not NULL, then set the next pointer of this previous node as
new_node, new_node->prev->next = new_node
Output:
Created DLL is:
Traversal in forward direction
1 7 8 6 4
Traversal in reverse direction
4 6 8 7 1
Algorithm
Let the node to be deleted is del.
1) If node to be deleted is head node, then change the head pointer to next current head.
2) Set next of previous to del, if previous to del exixts.
3) Set prev of next to del, if next to del exixts.
#include <stdio.h>
#include <stdlib.h>
struct Node
int data;
};
return;
if(*head_ref == del)
*head_ref = del->next;
if(del->next != NULL)
del->next->prev = del->prev;
if(del->prev != NULL)
del->prev->next = del->next;
free(del);
return;
}
/* UTILITY FUNCTIONS */
new_node->data = new_data;
new_node->prev = NULL;
(*head_ref) = new_node;
while(node!=NULL)
{
node = node->next;
}
int main()
push(&head, 4);
push(&head, 8);
push(&head, 10);
printList(head);
printList(head);
getchar();