PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next,*prev;
}node;
node *head=NULL,*newn,*temp,*t;
void insert();
void del();
void display();
void main()
{
int ch;
clrscr();
printf("\n\n$$$ DOUBLY LINKED LIST $$$\n\n");
while(1)
{
printf("\n\nDOUBLY LINKED LIST OPERATIONS ARE\n\n");
printf("1.Insert\n2.Delete\n3.Display\n4.Exit\n");
printf("Enter Ur Choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();break;
case 2:del();break;
case 3:display();break;
case 4:exit(0);
default:printf("Enter the correct option\n");
break;
}
}
}
void insert()
{
int no,op;
newn=(node *)malloc(sizeof(node));
printf("Enter the element to be inserted:\t");
scanf("%d",&newn->data);
printf("1.Insert at First\n2.Insert at Middle\n3.Insert at Last\n");
printf("Enter the option:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head==NULL)
{
newn->next=NULL;
newn->prev=NULL;
head=newn;
}
else
{
newn->next=head;
head->prev=newn;
head=newn;
newn->prev=NULL;
}
break;
case 2:
if(head->next==NULL)
{
printf("Single node entry so insertion is not allowed in the middle\n");
}
else
{
printf("Enter the data after which the element to be inserted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
temp=temp->next;
}
newn->next=temp->next;
temp->next->prev=newn;
temp->next=newn;
newn->prev=temp;
}
break;
case 3:
if(head==NULL)
{
newn->next=NULL;
newn->prev=NULL;
head=newn;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newn;
newn->prev=temp;
newn->next=NULL;
}
break;
}
}
void del()
{
int op,no;
if(head==NULL)
{
printf("List is empty\n");
}
else
{
printf("1.Deletion at first\n2.Deletion at middle\n3.Deletion at last\n");
printf("Enter your choice:\t");
scanf("%d",&op);
switch(op)
{
case 1:
if(head->next==NULL)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
head=head->next;
printf("The deleted element is: %d",temp->data);
free(temp);
}
break;
case 2:
if(head->next==NULL)
{
printf("Single node entry cannot perform deletion in the middle\n");
}
else
{
printf("Enter the element to be deleted:\t");
scanf("%d",&no);
temp=head;
while(temp->data!=no)
{
t=temp;
temp=temp->next;
}
temp->prev=t;
printf("The deleted element is: %d",temp->data);
t->next=temp->next;
temp->next->prev=t;
free(temp);
}
break;
case 3:
if(head->next==NULL)
{
printf("The deleted element is: %d",head->data);
head=NULL;
}
else
{
temp=head;
while(temp->next!=NULL)
{
t=temp;
temp=temp->next;
}
t->next=NULL;
printf("The deleted element is: %d",temp->data);
free(temp);
}
break;
}
}
}
void display()
{
if(head==NULL)
{
printf("List is empty\n");
}
else
{
temp=head;
printf("The elements in a list are:\n");
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
}
OUTPUT:
$$$ DOUBLY LINKED LIST $$$
DOUBLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 1
Enter the element to be inserted: 11
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option: 1
DOUBLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 1
Enter the element to be inserted: 33
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option: 3
DOUBLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 1
Enter the element to be inserted: 22
1.Insert at First
2.Insert at Middle
3.Insert at Last
Enter the option: 2
Enter the data after which the element to be inserted: 11
DOUBLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 3
The elements in a list are:
11
22
33
DOUBLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 2
1.Deletion at first
2.Deletion at middle
3.Deletion at last
Enter your choice: 2
Enter the element to be deleted: 22
The deleted element is: 22
DOUBLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 3
The elements in a list are:
11
33
DOUBLY LINKED LIST OPERATIONS ARE
1.Insert
2.Delete
3.Display
4.Exit
Enter Ur Choice: 4