1 #include <stdlib.h>
2
3 typedef struct note* link;
4 struct note{
5 int item;
6 link next;
7 };
8
9 void main(int argc, char* argv[]){
10 int i;
11 int N = atoi(argv[1]);
12 link t = malloc(sizeof *t); //起点的那个节点
13 link e = malloc(sizeof *e);
14 t->item = rand()%1000;
15 t->next = NULL;
16 e = t;
17 for(i = 1; i < N; i++){
18 link x = malloc(sizeof *x);
19 x->item = rand()%1000;
20 x->next = e->next;
21 e->next = x;
22 e = x;
23 }
24 e = t;
25 while (e->next != NULL){
26 printf("%d\n", e->item);
27 e = e->next;
28 }
29 printf("%d\n", e->item);
30 printf("\n");
31 e = t;
32 link s = malloc(sizeof *s); // 起点的那个节点
33 link d = malloc(sizeof *d); // 最后那个节点
34 link f = malloc(sizeof *d);
35 link p = malloc(sizeof *d); // 下一个节点
36 s->item = t->item;
37 s->next = NULL;
38 d = s;
39 link n = malloc(sizeof *n);
40 e = t;
41 for(i = 0; i< N; i++){
42 n = e;
43 p = e->next;
44 if(e->item < s->item){ // 添加到头节点
45 n->next = s;
46 s = n;
47 }else if(e->item > d->item){ // 添加到尾节点
48 n->next = NULL;
49 d->next = n;
50 d = n;
51 }else { // 添加到中间
52 f = s;
53 while(f->next != NULL){
54 if(f->next->item > e->item){
55 break;
56 }
57 f = f->next;
58 }
59 n->next = f->next;
60 f->next = n;
61 }
62 e = p;
63 }
64 while (s->next != NULL){
65 printf("%d\n", s->item);
66 s = s->next;
67 }
68 printf("%d\n", s->item);
69 }
算法c语言实现 3-11表插入排序
最新推荐文章于 2022-09-27 22:36:54 发布