Sie sind auf Seite 1von 4

Name of the Program:week2

Week2: Write a C program that uses functions to perform the following:


a) Create a doubly linked list of integers.
b) Delete a given integer from the above doubly linked list.
c) Display the contents of the above list after deletion.
#include<stdio.h>
#include<stdlib.h>
void create();
void insert();
void delete();
void display();
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node
*first=NULL,*last=NULL,*next,*prev,*cur;
void create()
{
if(first==NULL)
{
cur=(struct node*)malloc(sizeof (struct
node));
printf("\n enter the data:");
scanf("%d",&cur->data);
cur->left=NULL;
cur->right=NULL;
first=cur;
last=cur;
}
else
insert();
}
void insert()
{
int pos,c=1;
cur=(struct node*)malloc(sizeof (struct
node));
printf("\n enter data");
scanf("%d",&cur->data);
printf("\n enter the position");
scanf("%d",&pos);
if( (pos==1) && (first!=NULL))
{
cur->right=first;
cur->left=NULL;

first=cur;
}
else
{
next=first;
while(c<pos)
{
prev=next;
next=prev->right;
c++;
}
prev->right=cur;
cur->right=next;
}
}
void delete()
{
int pos,c=1;
printf("\n enter position:");
scanf("%d",&pos);
if(first==NULL)
printf("\n list is empty \n");
else if(pos==1&&first>right==NULL)
{
printf("\n deleted element is
%d \n ",first->data);
free(first);
first=NULL;
}
else if(pos==1&&first->right!
=NULL)
{
cur=first;
first=first->right;
cur->right=NULL;
printf("\n deleted element is
%d \n",cur->data);
free(cur);
}
else
{
next=first;
while(c<pos)

Name of the Program:week2


{

printf("\n enter your choice");


scanf("%d",&ch);
switch(ch)
{
case 1: create();
display();
break;
case 2: delete();
display();
break;
case 3: display();
break;
case 4: exit(0);
default:printf("invalid choice");
exit(0);
}
} while(1);

cur=next;
next=next->right;
c++;
}
cur->right=next->right;
next->right=NULL;
next->left=NULL;
printf("\n deleted element
is %d \n",next->data);
free(next);
}
}
void display()
{
struct node *temp;
temp=first;
if(temp==NULL)
printf("List is Empty");
while(temp!=NULL)
{
printf("<- %d-> ",temp->data);
temp=temp->right;
}
}
void main()
{
int ch;
printf("\n\n double linked list");
do
{
printf("\n1.create\n2.delete\n3.display\n4.exit"
);
.
enter data12
enter the position1
<- 12-> <- 23->
1.create
2.delete
3.display
4.exit
enter your choice1
enter data34

}
Output:
$ ./a.out
double linked list
1.create
2.delete
3.display
4.exit
enter your choice1
enter the data:23
<- 23->
1.create
2.delete
3.display
4.exit
enter your choice1
enter the position3
<- 12-> <- 23-> <- 34->
1.create
2.delete
3.display
4.exit
enter your choice1
enter data33
enter the position3
<- 12-> <- 23-> <- 33-> <- 34->
1.create
2.delete

Name of the Program:week2


3.display
4.exit
enter your choice2
invalid choice[13r21a0507@mysqldbserver
week2]$ vi doublelinked.c
[13r21a0507@mysqldbserver week2]$ cc
doublelinked.c
[13r21a0507@mysqldbserver week2]$ ./a.out
double linked list
1.create
2.delete
3.display
4.exit
enter your choice1

2.delete
3.display
4.exit
enter your choice2
enter position:3
deleted element is 33
<- 12-> <- 23-> <- 34->
1.create
2.delete
3.display
4.exit
enter your choice2
enter position:1

enter the data:23


<- 23->
1.create
2.delete
3.display
4.exit
enter your choice1

deleted element is12


<- 23-> <- 34->
1.create
2.delete
3.display
4.exit
enter your choice1

enter data12

enter data2

enter the position1


<- 12-> <- 23->
1.create
2.delete
3.display
4.exit
enter your choice1

enter the position3


<- 23-> <- 34-> <- 2->
1.create
2.delete
3.display
4.exit
enter your choice2

enter data34

enter position:2

enter the position3


<- 12-> <- 23-> <- 34->
1.create
2.delete
3.display
4.exit
enter your choice1

deleted element is 34
<- 23-> <- 2->
1.create
2.delete
3.display
4.exit
enter your choice2

enter data33

enter position:2

enter the position3


<- 12-> <- 23-> <- 33-> <- 34->
1.create

deleted element is 2
<- 23->
1.create

Name of the Program:week2


2.delete
3.display
4.exit
enter your choice2
enter position:1
deleted element is23
List is Empty
1.create
2.delete

3.display
4.exit
enter your choice3
List is Empty
1.create
2.delete
3.display
4.exit
enter your choice4

Das könnte Ihnen auch gefallen