单链表输出一直显示为0,无法显示输入的内容

单链表输出函数一直显示为0,无法输出数据。
刚学C语言,有许多地方不是很懂。

img

img

img

改为scanf("%d %s %s"),先输入数字,再输入字符串,空格分开。

输入部分分3条语句试试,不要在同一条语句,scanf那里

数据结构对单链表进行数据排序

修改如下,供参考:

#include <stdio.h>
#include <corecrt_malloc.h>
typedef struct Student {
    char  id[20];
    char  name[20];
    int   age;
    struct Student* next;
}Stu;

void create(Stu* head, int n)//Stu* create(Stu* head, int n)
{
    Stu* p = head, * q;//修改
    //p = p->next;修改
    int i = 0;
    for (i = 0; i < n; i++) {
        q = (Stu*)malloc(sizeof(Stu));//修改
        q->next = NULL;
        scanf("%s %s %d", q->id, q->name, &q->age);
        p->next = q;//修改
        p = q;      //修改 
    }
    //return p;//修改
}

void print(Stu* head)
{
    Stu* p = head;
    p = p->next;
    while (p) {
        printf("%s %s %d\n", p->id, p->name, p->age);
        p = p->next;
    }
}

int main()
{
    Stu* head = (Stu*)malloc(sizeof(Stu)), * x = (Stu*)malloc(sizeof(Stu));
    head->next = NULL;
    int n, i=0, id=0;
    printf("请输入需要添加的学生的数量和信息:");
    scanf("%d", &n);
    create(head, n); //head = create(&head, n);修改
    printf("打印学生信息:\n");
    print(head);        //print(&head);修改

    return 0;
}