Doubly Linked List
Doubly Linked List
Date: Signature:
A doubly linked list containing three nodes having numbers from 1 to 3 in their data part,
is shown in the following image.
in C, structure of a node in doubly linked list can be given as :
1. struct node
2. {
3. struct node *prev;
4. int data;
5. struct node *next;
6. }
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.
ALGORITHM
In the main function use switch case to make the program menu driven.
New node
2. Set prev and next pointers of new node point next of newNode to the first node of
the doubly linked list point prev to null
Reorganize the pointers (changes are denoted by purple arrows)
Point prev of the first node to newNode (now the previous head is the second node)
Write OVERFLOW
Go to Step 15
[END OF IF]
Step 6: SET I = 0
GOTO STEP 15
[END OF IF]
[END OF LOOP]
Write UNDERFLOW
Go to Step 7
[END OF IF]
[END OF LOOP]
Step 7: EXIT
.Display
3.Current will point to the next node in the list in each iteration.
PROGRAM BODY
#include<stdio.h> #include<stdlib.h>
struct node
int data;
};
struct node *head; void insertion (); void insertion_specified(); void deletion_last();
void display();
void main ()
printf("\n*********Main Menu*********\n");
break; case 3: deletion_last(); break; case 4: display(); case 9: exit(0); break; default:
void insertion()
{
struct node *ptr,*temp;
int item;
printf("\nOVERFLOW");
} else
head = ptr;
} else
temp = temp->next;
ptr->next = NULL;
printf("\nnode inserted\n");
}
void insertion_specified()
int item,loc,i;
if(ptr == NULL)
printf("\n OVERFLOW");
} else
for(i=0;i<loc;i++)
temp->next->prev=ptr;
printf("\nnode inserted\n");
void deletion_last()
printf("\n UNDERFLOW");
printf("\nnode deleted\n");
} else
ptr = head;
if(ptr->next != NULL)
}
}
void display()
struct node *ptr; printf("\n printing values...\n"); ptr = head; while(ptr != NULL)
printf("%d\n",ptr->data);
ptr=ptr->next;
OUTPUT
********Main Menu*********
Choose one option from the following list ... *********Main Menu*********
===============================================
1.Insert
4.Display
5.Exit
*********Main Menu*********
Choose one option from the following list ...
===============================================
1.Insert
4.Display
5.Exit
Node inserted
*********Main Menu*********
===============================================
1.Insert
4.Display
5.Exit
Node deleted
*********Main Menu*********
1.Insert
4.Display
5.Exit
12
*********Main Menu*********
===============================================
1.Insert
4.Display
5.Exit
DISCUSSION
Doubly Linked List is a variation of Linked list in which navigation is possible in both
ways, either forward and backward easily as compared to Single Linked List.
Following are the important terms to understand the concept of doubly linked list.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
Prev − Each link of a linked list contains a link to the previous link called Prev.
LinkedList − A Linked List contains the connection link to the first link called First
and to the last link called Last.
Basic Operations