#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
void append(struct node **q)
{
struct node *temp;
temp=*q;
if(temp==NULL)
{
temp=(struct node
*)malloc(sizeof(struct node ));
printf("\nenter the
data in Empty list : ");
scanf("%d",&temp->data);
temp->next=NULL;
*q=temp;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=(struct node
*)malloc(sizeof(struct node ));
temp=temp->next;
temp->next=NULL;
printf("\nenter the
data : ");
scanf("%d",&temp->data);
}
}
void sort(struct node **q)
{
struct node *a,*b,*temp;
a=*q;
while(a!=NULL)
{
b=a->next;
while(b!=NULL)
{
if(b->data>a->data)
{
temp->data=b->data;
b->data=a->data;
a->data=temp->data;
}
b=b->next;
}
a=a->next;
}
}
void display(struct node *q)
{
struct node *temp;
temp=q;
while(temp!=NULL)
{
printf("
%d",temp->data);
temp=temp->next;
}
}
void main()
{
struct node *start;
clrscr();
start=NULL;
append(&start);
append(&start);
append(&start);
append(&start);
printf("\nUnSorted
List :");
display(start);
sort(&start);
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
void append(struct node **q)
{
struct node *temp;
temp=*q;
if(temp==NULL)
{
temp=(struct node
*)malloc(sizeof(struct node ));
printf("\nenter the
data in Empty list : ");
scanf("%d",&temp->data);
temp->next=NULL;
*q=temp;
}
else
{
while(temp->next!=NULL)
temp=temp->next;
temp->next=(struct node
*)malloc(sizeof(struct node ));
temp=temp->next;
temp->next=NULL;
printf("\nenter the
data : ");
scanf("%d",&temp->data);
}
}
void sort(struct node **q)
{
struct node *a,*b,*temp;
a=*q;
while(a!=NULL)
{
b=a->next;
while(b!=NULL)
{
if(b->data>a->data)
{
temp->data=b->data;
b->data=a->data;
a->data=temp->data;
}
b=b->next;
}
a=a->next;
}
}
void display(struct node *q)
{
struct node *temp;
temp=q;
while(temp!=NULL)
{
printf("
%d",temp->data);
temp=temp->next;
}
}
void main()
{
struct node *start;
clrscr();
start=NULL;
append(&start);
append(&start);
append(&start);
append(&start);
printf("\nUnSorted
List :");
display(start);
sort(&start);
printf("\nSort list in Descending order");
display(start);
getch();
}
The following will be the output :
The following will be the output :
No comments:
Post a Comment