Sorting the elements of the linear linklist can in two types
Now we create the basic environment of the linklist.
#include<stdio.h> #include<conio.h> struct slinearlinklist { int i; struct slinearlinklist *next; }; typedef struct slinearlinklist node; //prototype declaration void *sort_data1(node *); void *sort_data2(node *); node *head=NULL; int main() { int c,i,pos; while(1) { printf(" 1. Ascending Order\n"); printf(" 2. Descending Order\n"); printf("25. Exit\n"); printf("Enter Your choice : "); scanf("%d",&c); switch(c) { case 1: sort_data1(head); // Ascending Order break; case 2: sort_data2(head); // Descending Order break; case 25: exit(0); } } }
void *sort_data1(node *l) { node *temp1,*temp2; int x; temp1=l; while(temp1->next!=NULL) { for(temp2=temp1->next;temp2!=NULL;temp2=temp2->next) { if(temp1->i>temp2->i) { x=temp1->i; temp1->i=temp2->i; temp2->i=x; } } temp1=temp1->next; } }
void *sort_data2(node *l) { node *temp1,*temp2; int x; temp1=l; while(temp1->next!=NULL) { for(temp2=temp1->next;temp2!=NULL;temp2=temp2->next) { if(temp1->i<temp2->i) { x=temp1->i; temp1->i=temp2->i; temp2->i=x; } } temp1=temp1->next; } }