青衫烟雨客 发表于 2021-10-29 10:05:38

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

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

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

struct Sorce
{
    int chinese;
    int math;
    int english;
};

struct Student
{
    int num;
    char name;
    struct Sorce*sorce;
    float average;
    struct Student*next;
};

void getInput(struct Student*student);
void getInput(struct Student*student)
{
    printf("请输入学号");
    scanf("%d",&student->num);
    printf("请输入名字");
    scanf("%s",student->name);
    printf("请输入语文数学英语成绩");
    scanf("%d %d %d",&student->sorce->chinese,&student->sorce->math,&student->sorce->english);
    student->average=(student->sorce->chinese+student->sorce->math+student->sorce->english)/3;
}

void addStudent(struct Student**list);
void addStudent(struct Student**list)
{
    struct Student*previous;
    struct Student*current;
    struct Student*new;
    current=*list;
    previous=NULL;
    new=(struct Student*)malloc(sizeof(struct Student));
    if(new==NULL)
    {
      printf("内存分配失败\n");
      exit(1);
    }
    getInput(new);
    while(current!=NULL && (current->average)<(new->average))
    {
      previous=current;
      current=current->next;
    }
    new->next=current;
    if(previous==NULL)
    {
      *list=new;
    }
    else
    {
      previous->next=new;
    }
}

void printStu(struct Student*list);
void printStu(struct Student*list)
{
    struct Student*current;
    current=list;
    while(current!=NULL)
    {
      printf("%d %s %d %d %d\n",current->num,current->name,current->sorce->chinese,current->sorce->math,current->sorce->english);
      current=current->next;
    }
    putchar('\n');
    free(current);
}
int main()
{
    struct Student*list=NULL;
    int ch;
    while(1)
    {
      printf("是否录入学生信息(Y/N)");
      do
      {
            ch=getchar();
      }
      while(ch!='Y' && ch!='N');
            if(ch=='Y')
      {
            addStudent(&list);
      }
      else
      {
                break;
      }
    }
    printStu(list);
    return 0;
}

青衫烟雨客 发表于 2021-10-29 10:22:02

运行的时候输入一个结构体就结束了,希望大佬帮我看看

jackz007 发表于 2021-10-29 11:10:40

本帖最后由 jackz007 于 2021-10-29 11:56 编辑

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

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

struct Sorce
{
    int chinese ;
    int math    ;
    int english ;
} ;

struct Student
{
    int num             ;
    char name       ;
    struct Sorce sorce; // 【修改】
    float average       ;
    struct Student*next ;
} ;

void getInput(struct Student*student);
void getInput(struct Student*student)
{
    printf("请输入学号");
    scanf("%d",&student->num);
    printf("请输入名字");
    scanf("%s",student->name);
    printf("请输入语文数学英语成绩");
    scanf("%d %d %d",&student->sorce.chinese,&student->sorce.math,&student->sorce.english); // 【修改】
    student->average=(student->sorce.chinese+student->sorce.math+student->sorce.english)/3; // 【修改】
}

void addStudent(struct Student**list);
void addStudent(struct Student**list)
{
    struct Student*previous;
    struct Student*current;
    struct Student*p;               // 【修改】
    current=*list;
    previous=NULL;
    p=(struct Student*)malloc(sizeof(struct Student));   // 【修改】
    if(p==NULL) // 【修改】
    {
      printf("内存分配失败\n");
      exit(1);
    }
    getInput(p); // 【修改】
    while(current!=NULL && (current->average)<(p->average))// 【修改】
    {
      previous=current;
      current=current->next;
    }
    p->next=current;// 【修改】
    if(previous==NULL)
    {
      *list=p;// 【修改】
    }
    else
    {
      previous->next=p;// 【修改】
    }
}

void printStu(struct Student*list);
void printStu(struct Student*list)
{
    struct Student*current;
    current=list;
    while(current!=NULL)
    {
      printf("%d %s %d %d %d\n",current->num,current->name,current->sorce.chinese,current->sorce.math,current->sorce.english);// 【修改】
      current=current->next;
    }
    putchar('\n');
    free(current);
}
int main()
{
    struct Student*list=NULL;
    int ch;
    while(1)
    {
      printf("是否录入学生信息(Y/N)");
      do
      {
            ch=getchar();
      }
      while(ch!='Y' && ch!='N');
            if(ch=='Y')
      {
            addStudent(&list);
      }
      else
      {
                break;
      }
    }
    printStu(list);
    return 0;
}

青衫烟雨客 发表于 2021-10-29 12:36:31

谢谢,太感谢了

青衫烟雨客 发表于 2021-10-30 00:08:13

jackz007 发表于 2021-10-29 11:10
改为:

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

大佬,要是我想用尾插法插入输出要怎么改呀,改了好久没改出来

jackz007 发表于 2021-10-30 00:12:28

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

       那就把你尾插法的代码贴出来看看。

青衫烟雨客 发表于 2021-10-30 09:48:40

jackz007 发表于 2021-10-30 00:12
那就把你尾插法的代码贴出来看看。

我又发了一个帖子,可以的话大佬帮我看看,那个就是我的想法
页: [1]
查看完整版本: 有没有大佬帮我看看为啥我这个单链表排序执行不了