按着课本的思路写的应该是没有错误但是为什么程序不能执行,希望有人能解答一下
程序目的是链表的构建和输出
#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;
}
}
题主上面的代码,没什么问题。
可能是以下原因:
1)程序正在运行,无法编译,上次运行的窗口未关闭。
程序窗口重复运行没有及时关闭,存在多个打开窗口,得一个个都关闭了再编译。
2)一个项目中有多个 xx.c 文件,将多余的xx.c文件改成头文件即可。
3)有函数拼写错误,如:printf拼写成prntf。scanf()写成scan()等。
4)main()函数拼写错误,如写成mian绵羊
如果检查了以上情况都无法解决,那一般是自定义函数出错
如果检查了所有都没有错,可以新建一个项目,复制粘贴代码试试(注意:路径中不要有中文,免得编译可能会出错)