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

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

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

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%
我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”

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

说明出野指针了
遇到类似的问题,一步一步的调,不要一下写那么多代码一起跑
先从打印输入开始,看输入正确赋值了吗
然后打印链表,看正确吗
然后每一步操作之后都打印,看到底哪一步指针飞了

img


 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", &y->ID, &y->score);
    
    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;
}