鱼C论坛

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

[已解决]链表排序

[复制链接]
发表于 2020-12-13 12:57:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>//头文件
  4. #define LEN sizeof(struct data)//student结构的大小
  5. struct data *creat();//创建链表
  6. void output(struct data *head);//打印链表
  7. void sort(struct data *head);//排序
  8. struct data *insert(struct data *,struct data *);//插入结点
  9. struct data *remove(struct data*head,int n);//删除节点
  10. int m;
  11. struct data{
  12.         int n;
  13.         struct data *next;
  14. }; //定义结构数组
  15. int N;//全局变量,用来记录存放了多少数据。
  16. int main()//主函数
  17. {
  18.         int remove_n;//定义存放需要删除的数值的变量·
  19.         struct data *stu,node;//声明结构指针变量
  20.         stu=creat();//函数返回链表第一个节点的地址
  21.         output(stu);//打印输出链表
  22.         printf("\n");//换行
  23.         system("pause");
  24.         sort(stu);//排序
  25.         printf("The result of ascending sorting:\n");
  26.         output(stu);//升序输出
  27.         return 0;
  28. }
  29. struct data *creat()//创建链表函数
  30. {
  31.         struct data *head;
  32.         struct data *p1,*p2;
  33.         p1=p2=(struct data*)malloc(LEN);//开辟一个长度为LEN的内存区
  34.         printf("Please enter a number:");
  35.         scanf("%d",&p1->n);
  36.         head=NULL;//初始化头指针
  37.         N=0;//初始化结点个数
  38.         while(p1->n!=-1)//当输入-1时停止
  39.         {
  40.                 N++;
  41.                 if(1==N)
  42.                 {
  43.                         head=p1;
  44.                 }
  45.                 else
  46.                 {
  47.                         p2->next=p1;//把p1所指的结点连接在p2所指的结点后面
  48.                 }
  49.                 p2=p1;
  50.                 p1=(struct data*)malloc(LEN);
  51.                 printf("Please enter a number:");
  52.                 scanf("%d",&p1->n);
  53.         }
  54.         p2->next=NULL;//结束
  55.         return head;//使函数返回头指针
  56. }
  57. void output(struct data *head)//定义打印输出链表函数
  58. {
  59.         struct data *p;//在函数中定义struct data类型的变量p
  60.         p=head;//使p指向第一个结点
  61.         if(NULL!=head)//若不是空表
  62.         {
  63.                 do
  64.                 {
  65.                         printf("%d ",p->n);//输出一个结点中的数值
  66.                         p=p->next;//p指向下一个结点
  67.                 }while(p!=NULL);//当p不是空地址
  68.         }
  69. }
  70. void sort(struct data *head)//定义排序函数
  71. {
  72.         struct data *p,*q,*r,*t;
  73.         int flag = 1;
  74.         while(flag){
  75.                 p = head;
  76.                 q = head->next;
  77.                 flag = 0;

  78.                 while(q != NULL){//循环终止条件
  79.                         r = q->next;
  80.                         if(r == NULL){//循环终止条件
  81.                                 break;
  82.                         }else if(q->n > r->n){
  83.                                 t = r->next;
  84.                                 q->next = t;
  85.                                 r->next = q;
  86.                                 p->next = r;
  87.                                 //重新排序
  88.                                 p = r;
  89.                                 q = p->next;
  90.                                 r = q->next;
  91.                                 //指向整体后移便于接下来排序
  92.                                 flag = 1;
  93.                         }else{
  94.                                 p = q;
  95.                                 q = p->next;//指向后移
  96.                         }
  97.                 }
  98.         }
  99. }
复制代码

怎样修改才能使第一个节点也参与排序呢?头结点怎么建立,作用是啥?
求助
最佳答案
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
回复

使用道具 举报

发表于 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
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-12-14 09:09:49 | 显示全部楼层
少个类型转化,小问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-8 03:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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