c语言 这个编程有什么问题? 怎么不能运行

void CreateList(struct stu *head)
{
struct stu=NULL;
struct stu *temp=head;
int i=1;
printf("请输入5位学生的成绩;");
while(i<=5)
{
struct stu *newNode=(struct stu*)malloc(sizeof(struct stu));
newNode->next=NULL;
newNode->index=i++;
scanf("%f",&newNode->score);
temp->next=newNode;
temp->newNode;
}
}
void ()
{
struct stu *head;
head=(struct stu*)malloc(sizeof(struct stu));
head->next=NULL;
CreateList(head);
}
void Insert(struct stu *head)
{
int index;
float score;
struct stu *NewNode=(struct stu *)malloc(sizeof(struct stu));
struct stu *p= head;
printf("请输入要插入的学生编号;");
scanf("%d",&index);
printf("请输入要插入的学生成绩:");
scanf("%f",&score);
NewNode->index=index;
NewNode->score=score;
NewNode->next=NULL;
printf("请输入要插入的位置;");
scanf("%d",&index);
while(index>1&&p!=NULL)
{
p=p->next;
index--;
}
NewNode->next=P->next;
p->next=NewNode;
}
E:\tc\c语言编程\p174.c(5) : warning C4013: 'printf' undefined; assuming extern returning int
E:\tc\c语言编程\p174.c(8) : warning C4013: 'malloc' undefined; assuming extern returning int
E:\tc\c语言编程\p174.c(8) : error C2027: use of undefined type 'stu'
E:\tc\c语言编程\p174.c(1) : see declaration of 'stu'
E:\tc\c语言编程\p174.c(9) : error C2037: left of 'next' specifies undefined struct/union 'stu'
E:\tc\c语言编程\p174.c(9) : error C2065: 'NULL' : undeclared identifier
E:\tc\c语言编程\p174.c(10) : error C2037: left of 'index' specifies undefined struct/union 'stu'

/*缺少下面两行include 文件*/
#include <stdio.h>
#include <stdlib.h>

/*少了下面的学生结构体定义*/
struct stu{
int index;
float score;
struct stu *next;
};

void CreateList(struct stu *head)
{
/*下面这行错误+没有用*/
// stu=NULL;
struct stu *temp=head;
int i=1;
printf("请输入5位学生的成绩;");
while(i<=5)
{
struct stu *newNode=(struct stu*)malloc(sizeof(struct stu));
newNode->next=NULL;
newNode->index=i++;
scanf("%f",&newNode->score);
temp->next=newNode;
/*下面这行错误*/
//temp->newNode;
temp=newNode;
}
}

/*下面第一行是你原来的,少了入口函数main*/
//void ()
int main()
{
struct stu *head;
head=(struct stu*)malloc(sizeof(struct stu));
head->next=NULL;
CreateList(head);

/*下面这行是我添的,main函数需要返回值*/
return 0;
}

void Insert(struct stu *head)
{
int index;
float score;
struct stu *NewNode=(struct stu *)malloc(sizeof(struct stu));
struct stu *p= head;
printf("请输入要插入的学生编号;");
scanf("%d",&index);
printf("请输入要插入的学生成绩:");
scanf("%f",&score);
NewNode->index=index;
NewNode->score=score;
NewNode->next=NULL;
printf("请输入要插入的位置;");
scanf("%d",&index);
while(index>1&&p!=NULL)
{
p=p->next;
index--;
}
NewNode->next=p->next;
p->next=NewNode;
}

直接拷贝上面的代码,可以运行.追问

恩 ,我按你的方法又试了一遍,错误少了很多,但是还有一个错误,你再看看,错在哪了?
p174.c
E:\tc\c语言编程\p174.c(30) : warning C4098: 'main' : 'void' function returning a value
E:\tc\c语言编程\p174.c(52) : error C2065: 'P' : undeclared identifier
E:\tc\c语言编程\p174.c(52) : error C2223: left of '->next' must point to struct/union
执行 cl.exe 时出错.

追答

你直接拷贝我的代码吧,你自己的代码倒数第三行NewNode->next=P->next;
P要小写,
还有你换个编译器把,无论是微软的visual studio还是linux的GCC,比TC好太多了,主要是TC是90年代初期的产物,那个时候根本没有C/C++标准,所以里面很多东西都不符合标准C++

追问

为什么运行时只有第一部分,即输入完五个成绩就结束了,不能执行后面的插入了,你运行看看是怎么回事!

追答

你自己没有调用,他怎么运行,自己看自己写的main函数里面的东西。。。。。。想运行插入,在main函数里面添加语句 Insert(head);
如果你还想把链表显示出来, 自己还要写个打印函数......,这个东西不是你写的?

int main()
{
struct stu *head;
head=(struct stu*)malloc(sizeof(struct stu));
head->next=NULL;
CreateList(head);
/*下面是最新添加的*/
Insert(head);

/*下面这行是我添的,main函数需要返回值*/
return 0;
}

追问

额,我今年暑假才借的学校的书,打算自学c语言的,可是太难了,我是照着书上的写的,可却是错得,哎,你再看看,我添了你加的,又出了错误。

p174.c(44) : error C2371: 'Insert' : redefinition; different basic types
执行 cl.exe 时出错.

追答

加QQ370576000把,在这里交流真心麻烦.

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-07-27
struct stu=NULL; 这句编译器无法理解我也无法理解追问

去掉这个呢,后面的哪个错了

追答

贴去掉之后的编译提示

追问

E:\tc\c语言编程\p174.c(5) : warning C4013: 'printf' undefined; assuming extern returning int
E:\tc\c语言编程\p174.c(8) : warning C4013: 'malloc' undefined; assuming extern returning int
E:\tc\c语言编程\p174.c(8) : error C2027: use of undefined type 'stu'
E:\tc\c语言编程\p174.c(1) : see declaration of 'stu'

追答

#include

你stu类没定义过

第2个回答  2011-07-27
1,你的struct stu 结构体未定义,
2,"NULL“字符未定义,
3,printf 未定义,缺少引用的命名空间
4,malloc 未定义,缺少引用的命名空间追问

怎么改

追答

1,你的结构体怎么定义我就不多说了,自己去定义;
2,#define NULL 0
意思是定义:null 为 0,
其他的如法炮制
3,#include 头文件里包含 printf 等 IO 输入输出操作
4,#include 头文件里包含 malloc 分配空间的操作

追问

不行啊,你运行试试,你改改运行试试,我实在是改不出来了!

第3个回答  2011-07-27
这个怎么可能运行?
相似回答