I was trying to sort a linked list by a method described below -

original-

5 98 4532 65

[ITERATION 1]

round 1

5 greater than 98 ? no. don't interchange.

98 greater than 45 ? yes. interchange.

now-

5 45 98 32 65

round 2

45 greater than 98 ? no. don't interchange.

98 greater than 32 ? yes. interchange.

now-

5 45 32 98 65

round 3

32 greater than 98 ? no. don't interchange.

98 greater than 65 ? yes. interchange.

now-

5 45 32 65 98

So, after ITERATION 1...the greatest number moves to the end of the list...

so, after ITERATION 4..the linked list will be sorted as

5 32 45 65 98

So, after each ITERATION the greatest number moves towards the right and the smallest numbers move towards the left...and a time comes when the list is totally sorted....

I wrote this code...but it isn't working....

wut could be the possible error ??

Code:#include <stdio.h> #include <stdlib.h> struct node { int data; struct node *link; }; struct node *insert(struct node *p,int n) { if (p==NULL) { p=(struct node *)malloc(sizeof(struct node)); p->data=n; p->link=NULL; } else p->link =insert(p->link,n); return(p); } struct node *sort(struct node *p) { struct node *curr; curr=p; int count=0; while (curr!=NULL) { count++; curr=curr->link; } int i; struct node *one,*two,*three; one=p; two=p->link; three=p->link->link; for (i=0;i<=count-1;i++) { for (i=0;i<count-3;i++) { if (one->data > two->data) { one->link=three; two->link=one; } if (two->data > three->data) { two->link=three->link; three->link=two; one->link=three; } one=two; two=three; three=three->link; } one=p; two=p->link; three=p->link->link; } return (p); } void printlist (struct node *p) { printf ("The data values in the list are \n"); while (p!=NULL) { printf ("%d \t",p->data); p=p->link; } } int main () { int n,i; int x;int z; struct node *start =NULL; while (1) { printf ("Enter the number of nodes to be created at the end of the list : \n"); scanf ("%d",&n); for (i=0;i<n;i++) { printf ("Enter the data values to be placed in a node at the end of the list \n"); scanf ("%d",&x); start=insert (start,x); } printf ("The list is created !! \n"); printlist (start); printf ("\n"); if (n==0) break; } printf ("SORTED DATA : \n"); start=sort(start); printlist (start); printf ("\n"); }