问题遇到的现象和发生背景
按着课本的思路写的应该是没有错误但是为什么程序不能执行,希望有人能解答一下
程序目的是链表的构建和输出
遇到的现象和发生背景,请写出第一个错误信息
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
#include<stdio.h>
#include<stdlib.h>
struct node
{
int ID,score;
struct node *next;
};
int main()
{
struct node *creat(struct node *head,int n);
void print(struct node *head);
struct node *head=NULL;
int n;
printf("please enter n length ");
scanf("%d",&n);
head=creat(head,n);
print(head);
}
struct node *creat(struct node *head,int n)
{
struct node *p,*q;
int i;
for(i=1;i<=n;i++){
q=(struct node*)malloc(sizeof(struct node));
printf("please enter the %dth ID score\n",i);
scanf("%d %d",&q->ID,&q->score);
q->next=NULL;
if(head==NULL){
head=q;
}else{
p->next=q;
}
p=q;
}
return head;
}
void print(struct node *head)
{
struct node *p=head;
while(p!=NULL){
printf("ID:%d\nscore:%d\n",p->ID,p->score);
p=p->next;
}
}