bronya1465124479 2022-12-21 17:26 采纳率: 82.6%
浏览 24
已结题

不知道为什么运行到一半出来一个弹窗,点击重试之后后面的程序不运行没有把链表输出直接结束了,希望有人解答一下

不知道为什么运行到一半出来一个弹窗,点击重试之后后面的程序不运行没有把链表输出直接结束了,希望有人解答一下

遇到的现象和发生背景,请写出第一个错误信息
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
    int ID, score;
    struct node* next;
};

int main()
{
    struct node* creat(struct node* head, int n);
    struct node *insert(struct node* head);
    void print(struct node* head);
    struct node* head = NULL;
    int n;
    printf("please enter n\n");
    scanf_s("%d", &n);
    head = creat(head, n);
    char str[10], a[10] = "yes", b[10] = "no";
    printf("Do you want to insert a student's score?(enter yes or no)\n");
    getchar();
    gets_s(str);
    if (strcmp(str, a) == 0 || strcmp(str, b) != 0) {
        head = insert(head);
        print(head);
    }
}
struct node* creat(struct node* head,int n)//链表的构造
{
    struct node* p = NULL, * q = NULL;
    int i;
    for (i = 1; i <= n; i++) {
        q = (struct node*)malloc(sizeof(struct node));
        printf("please enter %dth ID score\n", i);
        scanf_s("%d %d", &q->ID, &q->score);
        q->next = NULL;
        if (head == NULL) {
            head = q;
        }
        else {
            p->next = q;
        }
        p = q;
    }
    return head;
}
 struct node *insert(struct node* head)//链表插入新节点的函数(序号是按照小到大排序输入)
{
    struct node* y =NULL, * q = NULL,*p=NULL;
    y = (struct node*)malloc(sizeof(struct node));
    printf("please enter ID and score of the student\n");
    scanf_s("%d %d", &p->ID, &p->score);
    if (p->ID > y->ID) {     
        y->next = head;
        head = y;
        return head;
    }
    q = head;
    p = head->next;
    while (p != NULL && p->ID < y->ID) {
        q = p;
        p = p->next;
    }
    y->next = p;
    q->next = y;
    return head;
}
void print(struct node* head)//输出链表
{
    struct node *p = head;
    while (p != NULL) {
        printf("ID:%d  score:%d\n", p->ID, p->score);
        p = p->next;
    }
}

运行结果及详细报错内容

img

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%
我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”
  • 写回答

4条回答 默认 最新

  • 快乐鹦鹉 2022-12-21 17:32
    关注

    scanf_s("%d %d", &p->ID, &p->score);
    p都是NULL,你输入啥啊
    if (p->ID > y->ID) {
    y都是刚创建的节点,哪来的ID值可以比较?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 12月29日
  • 已采纳回答 12月21日
  • 创建了问题 12月21日

悬赏问题

  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题