Doublylinked List Operations
Doublylinked List Operations
Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as
well as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three parts:
node data, pointer to the next node in sequence (next pointer) , pointer to the previous node (previous
pointer). A sample node in a doubly linked list is shown in the figure.
A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown in the
following image.
struct node
int data;
The prev part of the first node and the next part of the last node will always contain null indicating end
in each direction.
In a singly linked list, we could traverse only in one direction, because each node contains address of the
next node and it doesn't have any record of its previous nodes. However, doubly linked list overcome
this limitation of singly linked list. Due to the fact that, each node of the list contains the address of its
previous node, we can find all the details about the previous node as well by using the previous address
stored inside the previous part of each node.
Memory Representation of a doubly linked list is shown in the following image. Generally, doubly linked
list consumes more space for every node and therefore, causes more expansive basic operations such as
insertion and deletion. However, we can easily manipulate the elements of the list since the list
maintains pointers in both the directions (forward and backward)
In the following image, the first element of the list that is i.e. 13 stored at address 1. The head pointer
points to the starting address 1. Since this is the first element being added to the list therefore the prev
of the list contains null. The next node of the list resides at address 4 therefore the first node contains 4
in its next pointer.
We can traverse the list in this way until we find any node containing null or -1 in its next part.
Node Creation
struct node
int data;
struct node *next;
};
All the remaining operations regarding doubly linked list are described in the following table.
Menu Driven Program in C to implement all the operations of doubly linked list
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
while(choice != 9)
printf("\n*********Main Menu*********\n");
printf("\n===============================================\n");
scanf("\n%d",&choice);
switch(choice)
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
void insertion_beginning()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
scanf("%d",&item);
if(head==NULL)
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
else
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
printf("\nNode inserted\n");
void insertion_last()
int item;
if(ptr == NULL)
{
printf("\nOVERFLOW");
else
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
else
temp = head;
while(temp->next!=NULL)
temp = temp->next;
temp->next = ptr;
ptr ->prev=temp;
ptr->next = NULL;
}
}
printf("\nnode inserted\n");
void insertion_specified()
int item,loc,i;
if(ptr == NULL)
printf("\n OVERFLOW");
else
temp=head;
scanf("%d",&loc);
for(i=0;i<loc;i++)
temp = temp->next;
if(temp == NULL)
return;
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
void deletion_beginning()
if(head == NULL)
printf("\n UNDERFLOW");
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
ptr = head;
free(ptr);
printf("\nnode deleted\n");
void deletion_last()
if(head == NULL)
printf("\n UNDERFLOW");
head = NULL;
free(head);
printf("\nnode deleted\n");
else
{
ptr = head;
if(ptr->next != NULL)
free(ptr);
printf("\nnode deleted\n");
void deletion_specified()
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
printf("\nCan't delete\n");
{
ptr ->next = NULL;
else
free(temp);
printf("\nnode deleted\n");
void display()
ptr = head;
while(ptr != NULL)
printf("%d\n",ptr->data);
ptr=ptr->next;
void search()
ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
scanf("%d",&item);
while (ptr!=NULL)
if(ptr->data == item)
flag=0;
break;
else
flag=1;
i++;
}
if(flag==1)
Output
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
Enter your choice?
printing values...
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
Node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
Node inserted
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
printing values...
1234
123
12
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
Enter value89
node inserted
*********Main Menu*********
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
Enter value12345
node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
printing values...
1234
123
12345
12
89
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
node deleted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
node deleted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
printing values...
123
12345
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
Enter your choice?
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
8
printing values...
123
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
123
item found at location 1
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
Can't delete
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert in begining
2.Insert at last
7.Search
8.Show
9.Exit
Exited..