算法c语言实现 3-11表插入排序

  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 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值