哪位大佬能帮忙修改一下看看哪出错了,链表的创建

2301_78224334 2023-11-15 16:42:33

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

struct Student {
    char name[20];
    float score;
    struct Student* next;

};
struct Student* creat() {
    struct Student* head, * p1, * p2;
    int n = 0;
    head=p1 = p2 = (struct Student*)malloc(sizeof(struct Student));
    printf("请输入学生的姓名和成绩:\n");
    scanf_s("%s%f", p1->name, &p1->score);
    while ((p1->score != 0)&&(p1->name[0]!='q'))
    {
        n++;
        if (n == 1)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
        p1 = (struct Student*)malloc(sizeof(struct Student));
        scanf_s("%s,%f", p1->name, &p1->score);
        return head;

    }
}
void print(struct Student* head)
{
    struct Student* p;
    p = head;
    if (head == NULL)
        printf("无法打印。");
    else
        do {
            printf("%s,%f", p->name, p->score);
            p = p->next;

        } while (p != NULL);
}
void sort(struct Student** head)
{
    struct Student* p;
    int temp;
    p = *head;
    if (head == NULL) {
        printf("无法排序");
    }
    else
    {
        while (p != NULL) {
            if (p->score > p->next->score) {
                temp = p->score;
                p->score = p->next->score;
                p->next->score = temp;
            }
     }
    }

}
    int main()
    {
        struct Student* head;
        head = creat();
        print(head);
        return 0;


    }

 

 

请输入学生的姓名和成绩:
sam
90
tom
sam,0.000000
D:\code\Project1\x64\Release\Project1.exe (进程 16392)已退出,代码为 -1073741819。
按任意键关闭此窗口. . .

...全文
66 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
探头qing_qing 2023-11-15
  • 打赏
  • 举报
回复

struct Student* creat() 这是个啥,你要创建一个链表,链表结构定义好了,你直接linklist list_create(){
linklist H;

H=(linklist)malloc(sizeof(listnode));
if( H == NULL){
    printf("create node failed\n");
    return NULL;    
}

H->data = 0;
H->next = NULL;
return H;

}这样就创建了

69,512

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧