鱼C论坛

 找回密码
 立即注册
查看: 1077|回复: 7

[已解决]大佬帮我看看单链表排序

[复制链接]
发表于 2021-10-30 09:28:15 | 显示全部楼层 |阅读模式

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

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

x
问题是输入学生成绩按大到小输入,大的打印在最上面
我的思路按照不同情况分析:

1,当list是空的,把p=list。
2,当list不是空的,p最小,头指针先指向p,p指向原来的信息域
3,当list不是空的,p最大,先找到尾部的current,然后指向p
4,当list不是空的,p要插入中间,先找到比p大的第一个数为current,上一个数位previous,previous指向怕,p指向current

运行时第二种情况排序打印小的在上面
运行第三种输入两个数据后就自动结束了


  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct Sorce
  4. {
  5.     int chinese;
  6.     int math;
  7.     int english;
  8. };

  9. struct Student
  10. {
  11.     int num;
  12.     char name[20];
  13.     struct Sorce sorce;
  14.     float average;
  15.     struct Student*next;
  16. };

  17. void getInput(struct Student*student);
  18. void getInput(struct Student*student)
  19. {
  20.     printf("请输入学号");
  21.     scanf("%d",&student->num);
  22.     printf("请输入名字");
  23.     scanf("%s",student->name);
  24.     printf("请输入语文数学英语成绩");
  25.     scanf("%d %d %d",&student->sorce.chinese,&student->sorce.math,&student->sorce.english);
  26.     student->average=(student->sorce.chinese+student->sorce.math+student->sorce.english)/3;
  27. }

  28. void addStudent(struct Student**list);
  29. void addStudent(struct Student**list)
  30. {
  31.     struct Student*previous;
  32.     struct Student*current;
  33.     struct Student*p;
  34.     struct Student*temp;
  35.     current=*list;
  36.     previous=NULL;
  37.     p=(struct Student*)malloc(sizeof(struct Student));
  38.     if(p==NULL)
  39.     {
  40.         printf("内存分配失败\n");
  41.         exit(1);
  42.     }
  43.     getInput(p);
  44.     while(current!=NULL && (current->average)<(p->average))
  45.     {
  46.         previous=current;
  47.         current=current->next;                  
  48.     }
  49.     if(current==NULL && previous==NULL)
  50.     {
  51.         *list=p;
  52.     }
  53.     else if(current!=NULL && previous==NULL)
  54.     {
  55.         temp=*list;
  56.         *list=p;
  57.         p->next=temp;
  58.     }
  59.     else if(current==NULL && previous!=NULL)
  60.     {
  61.         previous->next=p;
  62.         p->next=NULL;
  63.     }
  64.     else
  65.     {
  66.         previous->next=p;
  67.         p->next=current;
  68.     }
  69. }

  70. void printStu(struct Student*list);
  71. void printStu(struct Student*list)
  72. {
  73.     struct Student*current;
  74.     current=list;
  75.     while(current!=NULL)
  76.     {
  77.         printf("%d %s %d %d %d\n",current->num,current->name,current->sorce.chinese,current->sorce.math,current->sorce.english);
  78.         current=current->next;
  79.     }
  80.     putchar('\n');
  81.     free(current);
  82. }
  83. int main()
  84. {
  85.     struct Student*list=NULL;
  86.     int ch;
  87.     while(1)
  88.     {
  89.         printf("是否录入学生信息(Y/N)");
  90.         do
  91.         {
  92.             ch=getchar();
  93.         }
  94.         while(ch!='Y' && ch!='N');
  95.             if(ch=='Y')
  96.         {
  97.             addStudent(&list);
  98.         }
  99.         else
  100.         {
  101.                 break;
  102.         }
  103.     }
  104.     printStu(list);
  105.     return 0;
  106. }
复制代码
最佳答案
2021-10-30 10:16:38
本帖最后由 jackz007 于 2021-10-30 19:39 编辑

        改写这个函数
  1. void addStudent(struct Student**list)
复制代码

【正序】
  1. void addStudent(struct Student**list)
  2. {
  3.     struct Student * p , * p1 , * p2                  ;
  4.     p=(struct Student*)malloc(sizeof(struct Student)) ;
  5.     if(p==NULL)
  6.     {
  7.         printf("内存分配失败\n");
  8.         exit(1);
  9.     }
  10.     getInput(p);
  11.     for(p1 = p2 = * list ; p1 && p1 -> average < p -> average ; p2 = p1 , p1 = p2 -> next) ;
  12.     if(p2) {
  13.         if(p1 == * list) {
  14.             p -> next = p2     ; // 新节点位于链头
  15.             * list = p         ;
  16.         } else {
  17.             p -> next = p1     ; // 新节点位于链中、链尾
  18.             p2 -> next = p     ;
  19.         }
  20.     } else {
  21.         p -> next = NULL       ; // 新建链表
  22.         * list = p             ;
  23.     }
  24. }
复制代码

【逆序】:
  1. void addStudent(struct Student**list)
  2. {
  3.     struct Student * p , * p1 , * p2                  ;
  4.     p=(struct Student*)malloc(sizeof(struct Student)) ;
  5.     if(p==NULL)
  6.     {
  7.         printf("内存分配失败\n");
  8.         exit(1);
  9.     }
  10.     getInput(p);
  11.     for(p1 = p2 = * list ; p1 && p1 -> average > p -> average ; p2 = p1 , p1 = p2 -> next) ;
  12.     if(p2) {
  13.         if(p1 == * list) {
  14.             p -> next = p2     ; // 新节点位于链头
  15.              * list = p         ;
  16.         } else {
  17.             p -> next = p1     ; // 新节点位于链中、链尾
  18.             p2 -> next = p     ;
  19.         }
  20.     } else {
  21.         p -> next = NULL       ; // 新建链表
  22.         * list = p             ;
  23.     }
  24. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-30 09:29:16 | 显示全部楼层
本帖最后由 青衫烟雨客 于 2021-10-30 21:52 编辑

这道题题目是按照学号输入学生成绩,成绩不同,打印从成绩高到低,高的打印在最上面

第四种是previous指向p,p指向current
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-30 10:16:38 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-10-30 19:39 编辑

        改写这个函数
  1. void addStudent(struct Student**list)
复制代码

【正序】
  1. void addStudent(struct Student**list)
  2. {
  3.     struct Student * p , * p1 , * p2                  ;
  4.     p=(struct Student*)malloc(sizeof(struct Student)) ;
  5.     if(p==NULL)
  6.     {
  7.         printf("内存分配失败\n");
  8.         exit(1);
  9.     }
  10.     getInput(p);
  11.     for(p1 = p2 = * list ; p1 && p1 -> average < p -> average ; p2 = p1 , p1 = p2 -> next) ;
  12.     if(p2) {
  13.         if(p1 == * list) {
  14.             p -> next = p2     ; // 新节点位于链头
  15.             * list = p         ;
  16.         } else {
  17.             p -> next = p1     ; // 新节点位于链中、链尾
  18.             p2 -> next = p     ;
  19.         }
  20.     } else {
  21.         p -> next = NULL       ; // 新建链表
  22.         * list = p             ;
  23.     }
  24. }
复制代码

【逆序】:
  1. void addStudent(struct Student**list)
  2. {
  3.     struct Student * p , * p1 , * p2                  ;
  4.     p=(struct Student*)malloc(sizeof(struct Student)) ;
  5.     if(p==NULL)
  6.     {
  7.         printf("内存分配失败\n");
  8.         exit(1);
  9.     }
  10.     getInput(p);
  11.     for(p1 = p2 = * list ; p1 && p1 -> average > p -> average ; p2 = p1 , p1 = p2 -> next) ;
  12.     if(p2) {
  13.         if(p1 == * list) {
  14.             p -> next = p2     ; // 新节点位于链头
  15.              * list = p         ;
  16.         } else {
  17.             p -> next = p1     ; // 新节点位于链中、链尾
  18.             p2 -> next = p     ;
  19.         }
  20.     } else {
  21.         p -> next = NULL       ; // 新建链表
  22.         * list = p             ;
  23.     }
  24. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-30 13:26:42 | 显示全部楼层
jackz007 发表于 2021-10-30 10:16
改写这个函数

【头插法】

大佬,我想问问这个函数为啥当第一个输入数据小于第二个输入数据时,会进入死循环
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-30 16:00:33 | 显示全部楼层
jackz007 发表于 2021-10-30 10:16
改写这个函数

【头插法】

大佬,这道题还有方法实现嘛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-30 19:41:11 | 显示全部楼层
青衫烟雨客 发表于 2021-10-30 16:00
大佬,这道题还有方法实现嘛

       3 楼的代码已经更新,现在再试。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-30 21:06:01 | 显示全部楼层
jackz007 发表于 2021-10-30 19:41
3 楼的代码已经更新,现在再试。

可以了,非常感谢,麻烦您了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-30 21:48:43 | 显示全部楼层
本帖最后由 青衫烟雨客 于 2021-10-30 21:56 编辑

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-13 15:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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