include stdio include conio include stdlib define 20 struct list int v

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<stdio.h>
#include <conio.h>
#include <stdlib.h>
#define N 20
struct list
{ int value;
struct list *next;
};
list *Sort(int val,list *head)
{
list *p,*min,*tmp,*end,*head2=NULL;
while(head!=NULL)
{
min=p=head;
while(p->next!=NULL)
{
if(p->next->value<min->value)
{
tmp=p;
min=p->next;
}
p=p->next;
}
if(min==head) head=head->next;
else p->next=p->next->next;
if(head2==NULL) head2=end=min;
else
{
end->next=min;
end=end->next;
}
}
end->next=NULL;
return head2;
}
void Print_list(list *head2)
{
list *pnt = head2;
do
{
printf("%d ", pnt->value);
pnt=pnt->next;
}while(pnt!=head2);
}
void main()
{
char str[N];
int m ;
list *head = NULL;
puts("Enter number:");
for(;;)
{
gets(str);
if(!str[0])break;
head = Sort(atoi(str),head);
}
Print_list(head2);
}