-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistisort.c
62 lines (61 loc) · 878 Bytes
/
listisort.c
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
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
struct Elem {
struct Elem *prev, *next;
int v;
};
void psort(struct Elem* a , long n)
{
struct Elem* all = a;
for (long i = 1; i < n; i++)
{
struct Elem* al = all;
long l = i - 1;
int e = al->next->v;
while ((al->v > e) && l >= 0)
{
al->next->v = al->v;
l--;
al = al->prev;
}
al->next->v = e;
all = all->next;
}
}
int main()
{
long n;
scanf("%ld", &n);
struct Elem *p;
struct Elem *pf;
for (long i = 0; i<n;i++)
{
struct Elem *c = malloc(sizeof(struct Elem));
scanf("%d", &c->v);
if (i>0)
{
c->prev = p;
p->next = c;
}
else
{
pf = c;
}
p = c;
}
p->next = pf;
pf->prev = p;
psort(pf, n);
p = pf;
for (long i = 0; i<n;i++)
{
printf("%d\n", p->v);
struct Elem* c;
c= p;
if (i < n-1) p = p->next;
free(c);
}
return 0;
}