鱼C论坛

 找回密码
 立即注册
查看: 1288|回复: 6

[已解决]有没有大佬帮我看看为啥我这个单链表排序执行不了

[复制链接]
发表于 2021-10-29 10:05:38 | 显示全部楼层 |阅读模式

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

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

x
这道问题是输入学生信息成绩,按照平均分比较后排序
我的思路是用单链表,插入一个结构体,根据平均分找到比平均分大的第一个数的结构体,然后插入单链表

  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*new;
  34.     current=*list;
  35.     previous=NULL;
  36.     new=(struct Student*)malloc(sizeof(struct Student));
  37.     if(new==NULL)
  38.     {
  39.         printf("内存分配失败\n");
  40.         exit(1);
  41.     }
  42.     getInput(new);
  43.     while(current!=NULL && (current->average)<(new->average))
  44.     {
  45.         previous=current;
  46.         current=current->next;
  47.     }
  48.     new->next=current;
  49.     if(previous==NULL)
  50.     {
  51.         *list=new;
  52.     }
  53.     else
  54.     {
  55.         previous->next=new;
  56.     }
  57. }

  58. void printStu(struct Student*list);
  59. void printStu(struct Student*list)
  60. {
  61.     struct Student*current;
  62.     current=list;
  63.     while(current!=NULL)
  64.     {
  65.         printf("%d %s %d %d %d\n",current->num,current->name,current->sorce->chinese,current->sorce->math,current->sorce->english);
  66.         current=current->next;
  67.     }
  68.     putchar('\n');
  69.     free(current);
  70. }
  71. int main()
  72. {
  73.     struct Student*list=NULL;
  74.     int ch;
  75.     while(1)
  76.     {
  77.         printf("是否录入学生信息(Y/N)");
  78.         do
  79.         {
  80.             ch=getchar();
  81.         }
  82.         while(ch!='Y' && ch!='N');
  83.             if(ch=='Y')
  84.         {
  85.             addStudent(&list);
  86.         }
  87.         else
  88.         {
  89.                 break;
  90.         }
  91.     }
  92.     printStu(list);
  93.     return 0;
  94. }
复制代码
最佳答案
2021-10-29 11:10:40
本帖最后由 jackz007 于 2021-10-29 11:56 编辑
  1. struct Student
  2. {
  3.     int num;
  4.     char name[20];
  5.     struct Sorce*sorce ;
  6.     float average;
  7.     struct Student*next;
  8. };
复制代码

      改为:
  1. struct Student
  2. {
  3.     int num;
  4.     char name[20];
  5.     struct Sorce sorce ;
  6.     float average;
  7.     struct Student*next;
  8. };
复制代码

         如果不改,结构体的 sorce 成员只是一个指针,当定义该结构体变量时,此变量中并不包括结构体  struct Sorce 的存储空间,sorce 成员只是一个指向 NULL 的指针,相关的成员变量(Chinese、math、english)将没有空间保存,在访问这些变量的时候,将会导致异常退出。
         还有就是函数 void addStudent(struct Student**list) 中多处使用 new 作为变量名,这在我使用的 gcc 10.2.0 编译器会直接报错。
         代码问题主要集中在以上两点。

  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.     current=*list;
  35.     previous=NULL;
  36.     p=(struct Student*)malloc(sizeof(struct Student));   // 【修改】
  37.     if(p==NULL) // 【修改】
  38.     {
  39.         printf("内存分配失败\n");
  40.         exit(1);
  41.     }
  42.     getInput(p); // 【修改】
  43.     while(current!=NULL && (current->average)<(p->average))  // 【修改】
  44.     {
  45.         previous=current;
  46.         current=current->next;
  47.     }
  48.     p->next=current;  // 【修改】
  49.     if(previous==NULL)
  50.     {
  51.         *list=p;  // 【修改】
  52.     }
  53.     else
  54.     {
  55.         previous->next=p;  // 【修改】
  56.     }
  57. }

  58. void printStu(struct Student*list);
  59. void printStu(struct Student*list)
  60. {
  61.     struct Student*current;
  62.     current=list;
  63.     while(current!=NULL)
  64.     {
  65.         printf("%d %s %d %d %d\n",current->num,current->name,current->sorce.chinese,current->sorce.math,current->sorce.english);  // 【修改】
  66.         current=current->next;
  67.     }
  68.     putchar('\n');
  69.     free(current);
  70. }
  71. int main()
  72. {
  73.     struct Student*list=NULL;
  74.     int ch;
  75.     while(1)
  76.     {
  77.         printf("是否录入学生信息(Y/N)");
  78.         do
  79.         {
  80.             ch=getchar();
  81.         }
  82.         while(ch!='Y' && ch!='N');
  83.             if(ch=='Y')
  84.         {
  85.             addStudent(&list);
  86.         }
  87.         else
  88.         {
  89.                 break;
  90.         }
  91.     }
  92.     printStu(list);
  93.     return 0;
  94. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-29 10:22:02 | 显示全部楼层
运行的时候输入一个结构体就结束了,希望大佬帮我看看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-29 11:10:40 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2021-10-29 11:56 编辑
  1. struct Student
  2. {
  3.     int num;
  4.     char name[20];
  5.     struct Sorce*sorce ;
  6.     float average;
  7.     struct Student*next;
  8. };
复制代码

      改为:
  1. struct Student
  2. {
  3.     int num;
  4.     char name[20];
  5.     struct Sorce sorce ;
  6.     float average;
  7.     struct Student*next;
  8. };
复制代码

         如果不改,结构体的 sorce 成员只是一个指针,当定义该结构体变量时,此变量中并不包括结构体  struct Sorce 的存储空间,sorce 成员只是一个指向 NULL 的指针,相关的成员变量(Chinese、math、english)将没有空间保存,在访问这些变量的时候,将会导致异常退出。
         还有就是函数 void addStudent(struct Student**list) 中多处使用 new 作为变量名,这在我使用的 gcc 10.2.0 编译器会直接报错。
         代码问题主要集中在以上两点。

  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.     current=*list;
  35.     previous=NULL;
  36.     p=(struct Student*)malloc(sizeof(struct Student));   // 【修改】
  37.     if(p==NULL) // 【修改】
  38.     {
  39.         printf("内存分配失败\n");
  40.         exit(1);
  41.     }
  42.     getInput(p); // 【修改】
  43.     while(current!=NULL && (current->average)<(p->average))  // 【修改】
  44.     {
  45.         previous=current;
  46.         current=current->next;
  47.     }
  48.     p->next=current;  // 【修改】
  49.     if(previous==NULL)
  50.     {
  51.         *list=p;  // 【修改】
  52.     }
  53.     else
  54.     {
  55.         previous->next=p;  // 【修改】
  56.     }
  57. }

  58. void printStu(struct Student*list);
  59. void printStu(struct Student*list)
  60. {
  61.     struct Student*current;
  62.     current=list;
  63.     while(current!=NULL)
  64.     {
  65.         printf("%d %s %d %d %d\n",current->num,current->name,current->sorce.chinese,current->sorce.math,current->sorce.english);  // 【修改】
  66.         current=current->next;
  67.     }
  68.     putchar('\n');
  69.     free(current);
  70. }
  71. int main()
  72. {
  73.     struct Student*list=NULL;
  74.     int ch;
  75.     while(1)
  76.     {
  77.         printf("是否录入学生信息(Y/N)");
  78.         do
  79.         {
  80.             ch=getchar();
  81.         }
  82.         while(ch!='Y' && ch!='N');
  83.             if(ch=='Y')
  84.         {
  85.             addStudent(&list);
  86.         }
  87.         else
  88.         {
  89.                 break;
  90.         }
  91.     }
  92.     printStu(list);
  93.     return 0;
  94. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-29 12:36:31 | 显示全部楼层
谢谢,太感谢了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-30 00:08:13 | 显示全部楼层
jackz007 发表于 2021-10-29 11:10
改为:

         如果不改,结构体的 sorce 成员只是一个指针,当定义该结构体变量时,此变量中 ...

大佬,要是我想用尾插法插入输出要怎么改呀,改了好久没改出来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-30 00:12:28 | 显示全部楼层
青衫烟雨客 发表于 2021-10-30 00:08
大佬,要是我想用尾插法插入输出要怎么改呀,改了好久没改出来

       那就把你尾插法的代码贴出来看看。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-30 09:48:40 | 显示全部楼层
jackz007 发表于 2021-10-30 00:12
那就把你尾插法的代码贴出来看看。

我又发了一个帖子,可以的话大佬帮我看看,那个就是我的想法
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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