Você está na página 1de 4

12.

Write a C Program to support the following operations on a


doubly linked list where each node consists of integers:
a. Create a doubly linked list by adding each node at the
front.
b. Insert a new node to the left of the node whose key
value is read as an input.
c. Delete the node of a given data, if it is found, otherwise
desplay appropriate message/
d. Display the contents of the list.

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void display();
struct node
{
int info;
struct node*next;
struct node*prev;
};
struct node*f=NULL,*temp,*cur;
void insert()
{
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
printf("no memory\n");
else
{
printf("enter the element\n");
scanf("%d",&temp->info);
temp->next=temp->prev=NULL;
}
}
void in_front()
{
printf("inserting at the front of the list\n");
insert();
if(f==NULL)
{
f=temp;
return;
}
temp->next=f;
f->prev=temp;
f=temp;
display();
return;
}
void ins_key()
{
int n;
printf("enter element before which node is to be inserted\n");
scanf("%d",&n);
insert();
cur=f;
if(cur->info==n)
{
temp->next=cur;
cur->prev=temp;
f=temp;
display();
return;
}
while(cur->info!=n&&cur!=NULL)
cur=cur->next;
if(cur==NULL)
{
printf("element not found\n");
free(cur);
return;
}
cur->prev->next=temp;
temp->prev=cur->prev;
temp->next=cur;
cur->prev=temp;
display();
return;
}
void del_id()
{
int n;
printf("enter the element to be deleted\n");
scanf("%d",&n);
cur=f;
if(cur->info==n)
{
printf("node containing element %d is deleted\n",cur->info);
if(f->next==NULL)
{
free(cur);
f=NULL;
}
else
{
f=f->next;
f->prev=NULL;
free(cur);
}
display();
return;
}
while(cur->info!=n&&cur!=NULL)
cur=cur->next;
if(cur==NULL)
{
printf("element not found\n");
free(cur);
return;
}
cur->prev->next=cur->next;
cur->next->prev=cur->prev;
display();
return;
}
void display()
{
struct node *p;
if(f==NULL)
{
printf("list is empty\n");
return;
}
p=f;
printf("list\n");
while(p!=NULL)
{
printf("%d",p->info);
p=p->next;
}
return;
}
void main()
{
int ch=1;
clrscr();
while(ch)
{
printf("enter your choice\n");
printf("1:insert at front 2:insert to left of given data\n");
printf("3:delete 4:display 5:exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:in_front();
break;
case 2:ins_key();
break;
case 3:del_id();
break;
case 4:display();
break;
case 5:exit(0);
default:printf("invalid choice\n");
break;
}
}
getch();
}

OUTPUT
enter your choice
1:insert at front 2:insert to left of given data
3:delete 4:display 5:exit
inserting at the front of the list
enter the element
10
enter your choice
1:insert at front 2:insert to left of given data
3:delete 4:display 5:exit
2
enter element before which node is to be inserted
20
enter the element
30
element not found
enter your choice
1:insert at front 2:insert to left of given data
3:delete 4:display 5:exit
2
enter element before which node is to be inserted
10
enter the element
20
list
20
10
enter your choice
1:insert at front 2:insert to left of given data
3:delete 4:display 5:exit
4
list
20
10
enter your choice
1:insert at front 2:insert to left of given data
3:delete 4:display 5:exit
3
enter element to be deleted
10
list
20
enter your choice
1:insert at front 2:insert to left of given data
3:delete 4:display 5:exit
3
enter element to be deleted
10
element not found
enter your choice
1:insert at front 2:insert to left of given data
3:delete 4:display 5:exit
5

Você também pode gostar