写了个创建链表的函数,然后用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;
}
运行结果如下:(第一行为输入,第二行为输出)