Sie sind auf Seite 1von 3

Lab: Data Structures Lab

Program/Exp.no:

Roll no:
Sheet no:
Date:

Name of the Program:week1


Week1: Write a C program that uses functions to perform the following:
a) Create a singly linked list of integers.
b) Delete a given integer from the above linked list.
c) Display the contents of the above list after deletion.
#include<stdio.h>
{
#include<stdlib.h>
next=first;
void create();
while(c<pos)
void insert();
{
void delete();
prev=next;
void display();
next=prev->link;
struct node
c++;
{
}
int data;
if(prev==NULL)
struct node*link;
printf("\n invalid position\n");
};
else
struct node
{
*first=NULL,*last=NULL,*next,*prev,*cur;
cur->link=prev->link;
void create()
prev->link=cur;
{
}
if(first==NULL)
}
{
}
cur=(struct node*)malloc(sizeof (struct
void delete()
node));
{
printf("\n enter the data:");
int pos,c=1;
scanf("%d",&cur->data);
printf("\n enter position:");
cur->link=NULL;
scanf("%d",&pos);
first=cur;
if(first==NULL)
last=cur;
printf("\n list is empty \n");
}
else if(pos==1&&firstelse
>link==NULL)
insert();
{
}
printf("\n deleted element is
void insert()
%d \n ",first->data);
{
free(first);
int pos,c=1;
first=NULL;
cur=(struct node*)malloc(sizeof (struct
}
node));
else if(pos==1&&first->link!
printf("\n enter data");
=NULL)
scanf("%d",&cur->data);
{
printf("\n enter the position");
cur=first;
scanf("%d",&pos);
first=first->link;
if( (pos==1) && (first!=NULL))
cur->link=NULL;
{
printf("\n deleted element is
cur->link=first;
%d \n",cur->data);
first=cur;
free(cur);
}
}
else
else

Lab: Data Structures Lab


Program/Exp.no:

Roll no:
Sheet no:
Date:

Name of the Program:week1


{
next=first;
while(c<pos)
{
cur=next;
next=next->link;
c++;
}
cur->link=next->link;
next->link=NULL;
printf("\n deleted element
is %d \n",next->data);
free(next);
}
}
void display()
{
if(first==NULL)
printf("\n list is empty");
else
{
cur=first;
while(cur!=NULL)
{
printf("%d-->",cur->data);
cur=cur->link;
}
}

display();
break;
case 3: display();
break;
case 4: exit(0);
default:printf("invalid choice");
exit(0);
}
} while(1);
}
Output:
$ ./a.out
singly linked list
1.create
2.delete
3.display
4.exit
enter your choice1
enter the data:12
12-->
1.create
2.delete
3.display
4.exit
enter your choice1
enter data10

}
void main()
{
int ch;
printf("\n\n singly linked list");
do
{

enter the position1


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

printf("\n1.create\n2.delete\n3.display\n4.exit"
);
printf("\n enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
display();
break;
case 2: delete();

enter data23
enter the position3
10-->12-->23-->
1.create
2.delete
3.display
4.exit
enter your choice1

Lab: Data Structures Lab


Program/Exp.no:

Roll no:
Sheet no:
Date:

Name of the Program:week1


enter data4
enter the position4
10-->12-->23-->4-->
1.create
2.delete
3.display
4.exit
enter your choice2
enter position:3
deleted element is 23
10-->12-->4-->
1.create
2.delete
3.display
4.exit
enter your choice2
enter position:3

deleted element is 4
10-->12-->
1.create
2.delete
3.display
4.exit
enter your choice2
enter position:1
deleted element is10
12-->
1.create
2.delete
3.display
4.exit
enter your choice2
enter position:1
deleted element is12
list empty