鱼C论坛

 找回密码
 立即注册
查看: 844|回复: 2

[已解决]链表排序

[复制链接]
发表于 2020-12-13 16:28:41 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>

  4. struct data{
  5.         int n;
  6.         struct data *next;
  7. };

  8. #define LEN sizeof(struct data)

  9. int m ;
  10. int N ; //全局变量,用来记录存放了多少数据。

  11. struct data * creat(void)//创建链表函数
  12. {
  13.         struct data * head , * p1 , * p2                                             ;
  14.         int d , k                                                                    ;
  15.         for(N = 0 , k = 0 ;; k ++ , N ++) {
  16.                 printf("Please enter a number(-1 for ending input) : ")              ;
  17.                 scanf("%d" , & d)                                                    ;
  18.                 if(d != -1) {
  19.                         if((p2 = malloc(sizeof(struct data))) != NULL) {
  20.                                 p2 -> n = d                                          ;
  21.                                 p2 -> next = NULL                                    ;
  22.                                 if(! k) head = p2                                    ;
  23.                                 else p1 -> next = p2                                 ;
  24.                                 p1 = p2                                              ;
  25.                         } else {
  26.                                 fprintf(stderr , "\n")                               ;
  27.                                 fprintf(stderr , "\tError : can't alloc memory !\n") ;
  28.                                 fprintf(stderr , "\n")                               ;
  29.                                 exit(1)                                              ;
  30.                         }
  31.                 } else {
  32.                         break                                                        ;
  33.                 }
  34.                 printf("\n")                                                         ;
  35.         }
  36.         return head                                  ;
  37. }

  38. void output(struct data * head)//定义打印输出链表函数
  39. {
  40.         struct data * p                                           ;
  41.         for(p = head ; p ; p = p -> next) printf("%d\n" , p -> n) ;
  42. }

  43. struct data * sort(struct data * head)
  44. {
  45.         struct data * h , * p , * q , * r , * t                   ;
  46.         int f                                                     ;
  47.         for(f = 1 , h = head ; f ;) {
  48.                 f = 0                                             ;
  49.                 for(r = NULL , q = h , p = q -> next ; q && p ; r = q , q = p , p = q -> next) {
  50.                         if(q -> n > p -> n) {
  51.                                 t = p -> next                     ;
  52.                                 p -> next = q                     ;
  53.                                 q -> next = t                     ;
  54.                                 if(r) {
  55.                                         r -> next = p             ;
  56.                                 } else {
  57.                                         h = p                     ;
  58.                                 }
  59.                                 f ++                              ;
  60.                         }
  61.                 }
  62.         }
  63.         return h                                                  ;
  64. }

  65. int main()//主函数
  66. {
  67.         int remove_n;//定义存放需要删除的数值的变量·
  68.         struct data * stu , node;//声明结构指针变量
  69.         stu = creat() ;//函数返回链表第一个节点的地址
  70.         output(stu)   ;//打印输出链表
  71.         printf("\n");//换行
  72.         stu = sort(stu);//排序
  73.         printf("The result of ascending sorting:\n");
  74.         output(stu);//升序输出
  75. }
复制代码

        编译、运行实况
  1. D:\00.Excise\C>cl x.c
  2. 用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.28.29334 版
  3. 版权所有(C) Microsoft Corporation。保留所有权利。

  4. x.c
  5. Microsoft (R) Incremental Linker Version 14.28.29334.0
  6. Copyright (C) Microsoft Corporation.  All rights reserved.

  7. /out:x.exe
  8. x.obj

  9. D:\00.Excise\C>x
  10. Please enter a number(-1 for ending input) : 9

  11. Please enter a number(-1 for ending input) : 8

  12. Please enter a number(-1 for ending input) : 7

  13. Please enter a number(-1 for ending input) : 6

  14. Please enter a number(-1 for ending input) : 5

  15. Please enter a number(-1 for ending input) : 4

  16. Please enter a number(-1 for ending input) : 3

  17. Please enter a number(-1 for ending input) : 2

  18. Please enter a number(-1 for ending input) : 1

  19. Please enter a number(-1 for ending input) : -1
  20. 9
  21. 8
  22. 7
  23. 6
  24. 5
  25. 4
  26. 3
  27. 2
  28. 1

  29. The result of ascending sorting:
  30. 1
  31. 2
  32. 3
  33. 4
  34. 5
  35. 6
  36. 7
  37. 8
  38. 9

  39. D:\00.Excise\C>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-6 04:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表