39)Write a c program to sort element of a singly linked list in accending order.


#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*start;
void create(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(“\n\n LIST IS EMPTY”);
else
{
q=start;
while(q!=NULL)
{
printf(“%d->”,q->data);
q=q->link;
}
}
}
void sort()
{
struct list *q,*p;
int data;
q=start;
while(q!=NULL)
{
p=q->link;
while(p!=NULL)
{
if(q->data>p->data)
{
data=q->data;
q->data=p->data;
p->data=data;
}
p=p->link;
}
q=q->link;
}
}
void main()
{
int ch,m,i,n;
clrscr();
do
{
printf(“\n\n1.create \n\n2.display \n\n3.sort \n\n4.Exit\n\n”);
printf(“\n\nENTER UR CHOICE”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n\nHOW MANY NODES”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\n\nENTER THE DATA”);
scanf(“%d”,&m);
create(m);
}
break;
case 2:
disp();
break;
case 3:
sort();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}

2 thoughts on “39)Write a c program to sort element of a singly linked list in accending order.

  1. If you have copied the code in to your compiler and trying to run, it gives a stray error, in printf statements. Just delete the double quotation and add it again. Thank me later!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s