Doubly Linked List Class2
Doubly Linked List Class2
Doubly Linked List Class2
A doubly linked list is a two-way list because one can move in either
direction. That is, either from left to right or from right to left.
Insertion:
• Insertion of a node at the front
• Insertion of a node at any position in the list
• Insertion of a node at the end
Deletion:
• Deletion at front
• Deletion at any position
• Deletion at end
Display:
• Displaying/Traversing the elements of a list
header ptr
1010 1010 Before inserting a node at the beginning
NULL 50 NULL
2200 new
NULL 50 NULL
After inserting a node new
2200
at end of a list ptr 2000
new
NULL 10 2020 1010 20 1000 2020 30 2000 1000 40 2200
1010 2020 1000 2000
1. Create a new node
Algorithm:
2. Read the item
3. new->data=item
4. ptr= header
5. while(ptr->next!=NULL)
1. ptr=ptr->next; 2000 50 NULL
6. new->next=NULL; 2200
7. new->prev=ptr; new
8. ptr->next=new;
Insertion of a node at any position in the list
Algorithm: 1. create a node new
2. read item
3. new->data=item
4. ptr=header;
5. Read the position where the element is to be inserted
6. for(i=1;i<pos-1;i++)
6.1 ptr=ptr->next;
7. if(ptr->next = = NULL)
7.1 new->next = NULL;
7.2 new->prev=ptr;
7.3 ptr->next=new;
8. else
8.1 ptr1=ptr->next;
8.2 new->next=ptr1;
8.3 ptr1->prev=new;
8.4 new->prev=ptr;
8.5 ptr->next=new;
9. end
header
1010 1010
ptr ptr Before inserting a node at position 3
NULL 50 NULL
2200 new
header 2020
ptr ptr 1000
ptr ptr1
1010
2020 50 1000
2200 new
header
1010 Before deleting a node at beginning
Algorithm:
1.ptr=header
2.ptr1=ptr->next;
3.header=ptr1;
4.if(ptr1!=NULL)
1.ptr1->prev=NULL;
5. free(ptr);
header
1010 Before deleting a node at end
pt1 ptr
header
1000 2000
1010 After deleting a node at end
Algorithm:
1. ptr=header
2. while(ptr->next!=NULL)
1. ptr=ptr->next;
3. end while
4. ptr1=ptr->prev;
5. ptr1->next=NULL;
Deletion at any position
4. if(ptr = = header)
//if the deleted item is first node
Algorithm: 4.1 ptr1=ptr->next;
1. ptr=header; 4.2 ptr1->prev=NULL;
2. while(ptr->next!=NULL) 4.3 header=ptr1;
1.for(i=0;i<pos-1;i++) 4.4 end if
1. ptr=ptr->next; 5.else
2.if(i = = pos-1) 5.1 ptr1=ptr->prev;
1. break; 5.2 ptr2=ptr->next;
3. end while 5.3 ptr1->next=ptr2;
5.4 ptr2->prev=ptr1;
6. end else
7. end if
header Before deleting a node at position 3
1010 1010
ptr ptr
header 2020
ptr ptr1 1000 ptr ptr2 2000
1010
Forward Order : 10 20 30 40
Reverse Order : 40 30 20 10
/*Program to implement operations of double linked list*/
#include<stdio.h> #include<conio.h> #include<malloc.h>
void insertion(); void deletion(); void traverse(); int i,pos,item,choice;
struct node {
int data;
struct node *next;
struct node *prev;
}*new,*header,*ptr,*ptr1,*ptr2;
void main() {
header=NULL;
printf(" ***** MENU ****");
printf("\n1.Insertion \n2.Deletion \n3.Traverse \n4.Exit\n");
while(1) {
printf("\n\nEnter your choice: ");
scanf("%d",&choice);
switch(choice) {
case 1: insertion(); break;
case 2: deletion(); break;
case 3: traverse(); break;
case 4: exit();
default: printf("\nWrong choice");
}/* end of switch */
}/* end of while */
}/* end of main */
void insertion() {
ptr=header;
new=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the item to be inserted: ");
scanf("%d",&item);
new->data=item;
if(header==NULL) {
new->prev=NULL;
new->next=NULL;
header=new;
}
else {
printf("\nSelect the place:");
printf("\n1.Start \n2.Middle \n3.End\n");
scanf("%d",&choice);
if(choice==1) {
new->next=ptr;
ptr->prev=new;
new->prev=NULL;
header=new;
}/* choice1 */
if(choice==2)
{
printf("\nEnter the position to place the new element: ");
scanf("%d",&pos);
for(i=1;i<pos-1;i++)
ptr=ptr->next;
if(ptr->next==NULL) if(choice==3)
{ {
new->next=NULL; while(ptr->next!=NULL)
new->prev=ptr; ptr=ptr->next;
ptr->next=new; new->next=NULL;
} new->prev=ptr;
else ptr->next=new;
{ }
ptr1=ptr->next; }/* end of else */
new->next=ptr1; }/* end of insertion */
ptr1->prev=new;
new->prev=ptr;
ptr->next=new;
}
}/* choice2 */
void deletion()
{
ptr=header;
if(header==NULL)
printf("The list is empty\n");
else
{
printf("\Select the place:");
printf("\n1.Start \n2.Middle \n3.End\n");
scanf("%d",&choice);
if(choice==1)
{
printf("\nThe deleted item is: %d",ptr->data);
ptr1=ptr->next;
header=ptr1;
if(ptr1!=NULL)
ptr1->prev=NULL;
}/* choice1 */
if(choice==2) {
printf("\nEnter the position to delete the element: ");
scanf("%d",&pos);
while(ptr->next!=NULL) {
for(i=0;i<pos-1;i++)
ptr=ptr->next;
if(i==pos-1)
break;
}//while
printf("\n\nThe deleted node is: %d",ptr->data);
if(ptr==header)//deleted item is starting node
{
ptr1=ptr->next;
ptr1->prev=NULL; if(choice==3)
header=ptr1; {
}//if while(ptr->next!=NULL)
else { ptr=ptr->next;
ptr1=ptr->prev; printf("\n\nThe deleted node is: %d",ptr->data);
ptr2=ptr->next; ptr1=ptr->prev;
ptr1->next=ptr2; ptr1->next=NULL;
ptr2->prev=ptr1; }/* choice3 */
} }/*end of deletion */
}/* choice2 */
}/* end of else */
void traverse(){
ptr=header;
if(header==NULL)
printf("The list is empty\n");
else {
printf("\n\nThe elements in farword order: ");
while(ptr!=NULL) {
printf(" %d",ptr->data);
if(ptr->next==NULL) {
break;
}
ptr=ptr->next;
}/* end of while */
printf("\n\nThe elements in reverse order: ");
while(ptr!=header) {
if(ptr->next==NULL)
printf(" %d",ptr->data);
else
printf(" %d",ptr->data);
ptr=ptr->prev;
}/* end of while */
printf(" %d",ptr->data);
}/* end of else */
}/* end of traverse() */
Circular linked list
The linked list where the last node points the header node is called
circular linked list.