为什么创建链表、再输出链表后会多出一串数字?

写了个创建链表的函数,然后用main函数输出了一下,但是输出的第一个数字总是一个很大的数

代码如下:

#include <stdio.h>

struct student {
    int num;
    struct student* next;
};
typedef struct student stu;

stu* listCreate(int n){
    stu *head,*node,*end;
    head = (stu*)malloc(sizeof(stu));
    end = head;
    for(int i=0;i<n;i++){
        node = (stu*)malloc(sizeof(stu));
        scanf("%d",&node->num);
        end->next = node;
        end = node;
    }
    end->next = NULL;
    return head;
}

int main()
{
    stu* n = listCreate(5);
    while(1){
        printf("%d ",n->num);
        if (n->num != NULL)
        {
            n = n->next;
        }else
        {
            break;
        }
        
    }   
    
    return 0;
}

运行结果如下:(第一行为输入,第二行为输出)

img

因为第一个是头节点,你从来没写过值,会是一个随机值

因为你这个是哨兵链表,第一个节点的数据是无意义的,不用输出

int main()
{
    stu* n = listCreate(5);
    stu *p = n->next;
    while(1){
        printf("%d ",p->num);
        if (p->next != NULL)
        {
            p = p->next;
        }else
        {
            break;
        }
        
    }   
    
    return 0;
}


你头结点初始化后貌似没赋值