Solution:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*start=NULL;
void creat(int);
void swap();
void disp();
int main()
{
int ch,i,n,m;
do
{
printf("\n\n1.Create Linked list");
printf("\n2.Display");
printf("\n3.Swap");
printf("\n4.Exit\n");
printf("\nEnter your choice :\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter number of nodes :\n");
scanf("%d",&n);
printf("\nEnter elements of Linked list:\n");
for(i=0;i<n;i++)
{
scanf("%d",&m);
creat(m);
}
break;
case 2:
disp();
break;
case 3:
swap();
break;
case 4:
exit(0);
}
}while(ch!=4);
return 0;
}
void creat(int m)
{
struct list *tmp,*q;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
q=start;
while(q->link!=NULL)
{
q=q->link;
}
q->link=tmp;
}
}
void disp()
{
struct list *q;
if(start==NULL)
{
printf("list is empty");
}
else
{
q=start;
printf("\n");
while(q!=NULL)
{
printf("%d\n",q->data);
q=q->link;
}
}
}
void swap()
{
int m,n,i,tmp;
struct list *q,*ptr,*ptr1;
printf("\nEnter the mth and nth position:\n");
scanf("%d%d",&m,&n);
for(i=1,ptr=start;i<m && ptr!=NULL;ptr=ptr->link,i++);
for(i=1,ptr1=start;i<n && ptr1!=NULL;ptr1=ptr1->link,i++);
if(ptr!=NULL && ptr1!=NULL)
{
tmp=ptr->data;
ptr->data=ptr1->data;
ptr1->data=tmp;
}
else
{
printf("\nPosition Not Found");
}
}
0 Comments