请大家帮忙试试这个链表哪里错了100分
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long id;
int s1,s2,s3;
float aver;
struct student *next;
};
int n;
struct student *creat()
{struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld%d%d%d",&p1->id,&p1->s1,&p1->s2,&p1->s3);
p1->aver=(p1->s1+p1->s2+p1->s3)/3;
head=NULL;
while(p1->id!=0)
{n=n+1;
if (n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld%d%d%d",&p1->id,&p1->s1,&p1->s2,&p1->s3);
p1->aver=(p1->s1+p1->s2+p1->s3)/3;
}
p2->next=NULL;
return(head);
}
void print(head)
struct student *head;
{struct student *p;
int i;
p=head;
if (head!=NULL)
for (i=1;i<=n;i++)
{printf("%ld %d %d %d %f\n",p->id,p->s1,p->s2,p->s3,p->aver);
p=p->next;
}
}
main()
{
print(creat());
}
问题点数:0、回复次数:9Top
1 楼herryhuang(Herry)回复于 2005-06-04 14:38:25 得分 0
你的函数呢??????????
creat函数在哪?另外,换一个名字,这个函数名是标准库函数。Top
2 楼mingsi(Mingsi)回复于 2005-06-04 14:49:48 得分 0
函数在这里~struct student *creat()
换了也不行~我输入两组数据
1 2 3 4
2 3 4 5
0 0 0 0 (遇零结束)
结果输出是:
0 0 0 0 0.000000
178061594 395 112 0.000000
Top
3 楼mccxj(老鼠不逛街)回复于 2005-06-04 15:00:32 得分 0
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long id;
int s1,s2,s3;
float aver;
struct student *next;
};
struct student *head;
int n;
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=(struct student *)malloc(LEN);
p2=(struct student *)malloc(LEN);
scanf("%ld%d%d%d",&p1->id,&p1->s1,&p1->s2,&p1->s3);
p1->aver=float(p1->s1+p1->s2+p1->s3)/3;
head=NULL;
while(p1->id!=0)
{
n=n+1;
if (n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld%d%d%d",&p1->id,&p1->s1,&p1->s2,&p1->s3);
p1->aver=float(p1->s1+p1->s2+p1->s3)/3;
}
p2->next=NULL;
return(head);
}
void print(student *head)
{
struct student *p;
int i;
p=head;
if (head!=NULL)
for (i=1;i<=n;i++)
{
printf("%ld %d %d %d %f\n",p->id,p->s1,p->s2,p->s3,p->aver);
p=p->next;
}
}
int main()
{
print(creat());
return 0;
}
。。。你的格式和设计似乎有点问题。。Top
4 楼mccxj(老鼠不逛街)回复于 2005-06-04 15:02:16 得分 0
1 2 3 4
2 3 4 5
0 0 0 0 (遇零结束)
1 2 3 4 3.000000
2 3 4 5 4.000000
Press any key to continue
上面是测试结果。。环境:vc6.0Top
5 楼zhousqy(标准C匪徒)(甩拉,甩拉)回复于 2005-06-04 15:20:10 得分 0
运行结果,基本马马虎虎,没什么错阿。Top
6 楼mingsi(Mingsi)回复于 2005-06-04 15:39:56 得分 0
。。。难道是我的tc有问题~~~????Top
7 楼ice_yezh(椰树)回复于 2005-06-04 16:15:21 得分 0
运行结果没有问题呀!!Top
8 楼darkstar21cn(≮天残≯无畏)(死亡进行时)回复于 2005-06-04 17:05:33 得分 0
结构不太好,改一下:
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=(struct student *)malloc(LEN);
scanf("%ld%d%d%d",&p1->id,&p1->s1,&p1->s2,&p1->s3);
p1->aver=float(p1->s1+p1->s2+p1->s3)/3;
head=p2=p1;
while(p1->id!=0)
{
p1=(struct student *)malloc(LEN);
scanf("%ld%d%d%d",&p1->id,&p1->s1,&p1->s2,&p1->s3);
p1->aver=float(p1->s1+p1->s2+p1->s3)/3;
p2->next=p1;
p2=p1;
n=n+1;
}
p2->next=NULL;
return(head);
}
void print(student *head)
{
struct student *p=head;
while (p!=NULL)
{
printf("%ld %d %d %d %f\n",p->id,p->s1,p->s2,p->s3,p->aver);
p=p->next;
}
}Top
9 楼mostideal(三甲)回复于 2005-06-05 13:43:16 得分 0
先顶。。。Top




