16. Program to perform
linked list operations?
Output
Program
#include<stdio.h>
#include<conio.h>
int ch,i,item,n,pos;
char c;
struct node
{
int data;
struct node *link;
}*start,*newnode,*temp,*prev;
/*----Creating a node in the linked list-----*/
/*----Creating a node in the linked list-----*/
void createnode()
{
printf("\nEnter the data:\t");
scanf("%d",&item);
newnode=(struct node *)malloc(sizeof(temp));
newnode->data=item;
newnode->link=NULL;
}
/*-----------Creating the linked list----------*/
/*-----------Creating the linked list----------*/
void createlist()
{
printf("Enter the number of nodes:\t");
scanf("%d",&n);
start=NULL;
for(i=0;i<n;i++)
{
createnode();
if(start==NULL)
{
start=newnode;
}
else
{
temp=start;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=newnode;
}
}
}
/*----------Inserting at beginning--------*/
/*----------Inserting at beginning--------*/
void insbeg()
{
createnode();
newnode->link=start;
start=newnode;
}
/*---------Inserting at end------------*/
/*---------Inserting at end------------*/
void insend()
{
createnode();
if(start==NULL)
{
start=newnode;
}
else
{
temp=start;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=newnode;
}
}
/*----------Inserting in between the nodes-----------*/
/*----------Inserting in between the nodes-----------*/
void insbet()
{
printf("\nEnter the position of insertion:\t");
scanf("%d",&pos);
createnode();
temp=start;
for(i=0;i<pos-1;i++)
{
prev=temp;
temp=temp->link;
}
prev->link=newnode;
newnode->link=temp;
}
/*------------Displaying the linked list----------*/
/*------------Displaying the linked list----------*/
void display()
{
if(start==NULL)
{
printf("\nList Empty\n");
return;
}
else
{
temp=start;
printf("\nThe elements of linked list are:\n");
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->link;
}
printf("\n");
}
}
/*----------Insert operations--------*/
/*----------Insert operations--------*/
void ins()
{
do
{
printf("\nEnter the choice:\t1.Insert at beginning\t\t2.Insert at
end\n\t\t\t3.Insert in between nodes\t4.Display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insbeg();
break;
case 2:
insend();
break;
case 3:
insbet();
break;
case 4:
display();
break;
default:
printf("\nwrong choice\n");
break;
}
printf("\nDo you want to continue:(y/n)?\n");
scanf(" %c",&c);
}
while((c=='y')||(c=='Y'));
}
/*-----------Deleting from the beginning----------*/
/*-----------Deleting from the beginning----------*/
void delbeg()
{
if(start==NULL)
{
printf("\nNo Nodes\n");
}
else
{
temp=start;
printf("The deleted element is %d",temp->data);
start=temp->link;
free(temp);
}
}
/*----------Deleting from the end----------*/
/*----------Deleting from the end----------*/
void delend()
{
if(start==NULL)
{
printf("\nNo Nodes\n");
}
else
{
temp=start;
if(temp->link==NULL)
{
printf("The deleted element is %d",temp->data);
start=NULL;
free(temp);
}
else
{
while(temp->link!=NULL)
{
prev=temp;
temp=temp->link;
}
printf("The deleted element is %d",temp->data);
prev->link=NULL;
free(temp);
}
}
}
/*-----------Deleting in between the nodes-----------*/
/*-----------Deleting in between the nodes-----------*/
void delbet()
{
printf("\nEnter the position of deletion:\t");
scanf("%d",&pos);
temp=start;
for(i=0;i<pos-1;i++)
{
prev=temp;
temp=temp->link;
}
printf("The deleted element is %d",temp->data);
prev->link=temp->link;
free(temp);
}
/*-----------Delete operations---------*/
/*-----------Delete operations---------*/
void del()
{
do
{
printf("\nEnter the choice:\t1.Deletion at beginning\t\t2.Deletion at
end\n\t\t\t3.Deletion in between nodes\t4.Display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
delbeg();
break;
case 2:
delend();
break;
case 3:
delbet();
break;
case 4:
display();
break;
default:
printf("\nwrong choice\n");
break;
}
printf("\nDo you want to continue:(y/n)?\n");
scanf(" %c",&c);
}
while((c=='y')||(c=='Y'));
}
/*----------Searching in the linked list--------*/
/*----------Searching in the linked list--------*/
void search()
{
int x,i=0,c=0;
printf("\nEnter the number to be searched:\t");
scanf("%d",&x);
temp=start;
if(start==NULL)
{
printf("\nList is Empty\n");
return;
}
while(temp!=NULL)
{
i++;
if(temp->data==x)
{
c++;
printf("\nThe element is found in the %d node",i);
}
temp=temp->link;
}
if(c==0)
{
printf("\nElement not found\n");
}
}
/*---------Counting the number of nodes-------*/
/*---------Counting the number of nodes-------*/
void count()
{
int c=0;
temp=start;
if(start==NULL)
{
printf("\nList is Empty\n");
return;
}
while(temp!=NULL)
{
c++;
temp=temp->link;
}
printf("\nThe number of nodes is %d",c);
}
void main()
{
clrscr();
createlist();
do
{
printf("Enter the choice:\t1.Insertion\t2.Deletion\t
3.Count\n\t\t\t4.Search\t5.Display\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
ins();
break;
case 2:
del();
break;
case 3:
count();
break;
case 4:
search();
break;
case 5:
display();
break;
default:
printf("wrong choice\n");
break;
}
printf("\nDo you want to continue:(y/n)?\n");
scanf(" %c",&c);
}while((c=='y')||(c=='Y'));
Output
Enter the number of nodes: 2
Enter the data: 11
Enter the data: 13
Enter the choice: 1.Insertion 2.Deletion 3.Count
4.Search 5.Display
1
Enter the choice: 1.Insert at beginning 2.Insert at end
3.Insert in between nodes 4.Display
1
Enter the data: 10
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert at beginning 2.Insert at end
3.Insert in between nodes 4.Display
4
The elements of linked list are:
10 11 13
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert at beginning 2.Insert at end
3.Insert in between nodes 4.Display
2
Enter the data: 14
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert at beginning 2.Insert at end
3.Insert in between nodes 4.Display
4
The elements of linked list are:
10 11 13 14
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert at beginning 2.Insert at end
3.Insert in between nodes 4.Display
3
Enter the position of insertion: 3
Enter the data: 12
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insert at beginning 2.Insert at end
3.Insert in between nodes 4.Display
4
The elements of linked list are:
10 11 12 13 14
Do you want to continue:(y/n)?
n
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion 2.Deletion 3.Count
4.Search 5.Display
3
The number of nodes is 5
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion 2.Deletion 3.Count
4.Search 5.Display
4
Enter the number to be searched: 12
The element is found in the 3 node
Do you want to continue:(y/n)?
y
Enter the choice: 1.Insertion 2.Deletion 3.Count
4.Search 5.Display
2
Enter the choice: 1.Deletion at beginning 2.Deletion at end
3.Deletion in between nodes 4.Display
1
The deleted element is 10
Do you want to continue:(y/n)?
y
Enter the choice: 1.Deletion at beginning 2.Deletion at end
3.Deletion in between nodes 4.Display
4
The elements of linked list are:
11 12 13 14
Do you want to continue:(y/n)?
y
Enter the choice: 1.Deletion at beginning 2.Deletion at end
3.Deletion in between nodes 4.Display
2
The deleted element is 14
Do you want to continue:(y/n)?
y
Enter the choice: 1.Deletion at beginning 2.Deletion at end
3.Deletion in between nodes 4.Display
4
The elements of linked list are:
11 12 13
Do you want to continue:(y/n)?
y
Enter the choice: 1.Deletion at beginning 2.Deletion at end
3.Deletion in between nodes 4.Display
3
Enter the position of deletion: 2
The deleted element is 12
Do you want to continue:(y/n)?
y
Enter the choice: 1.Deletion at beginning 2.Deletion at end
3.Deletion in between nodes 4.Display
4
The elements of linked list are:
11 13
Do you want to continue:(y/n)?
n
Do you want to continue:(y/n)?
n
No comments:
Post a Comment