| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 423 人关注过本帖
标题:[求助]链表,出了错,却总想不通为什么错了
只看楼主 加入收藏
行走
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-7-26
收藏
 问题点数:0 回复次数:4 
[求助]链表,出了错,却总想不通为什么错了
用链表生成一个学生成绩表.
编译时提示出了两个错.代码如下:
#define NULL 0
#define LEN sizeof(struct ks)
#include<stdio.h>
#include<stdlib.h>
int n;
struct ks /*结构体声明*/
{
long num;
int score;
struct ks *next;
}
struct ks *create() /*第一个错误,“Too many types in declaration."*/
{
struct ks *head,*p1,*p2;
n=0;head=p2=NULL;
p1=(struct ks *)malloc(LEN); /*为一个结构体开辟一个空间*/
p1->next=NULL;
scanf("%ld%d",&p1->num,&p1->score);
while(p1->num!=0)
{
++n;
if(n==1)head=p1; /*第一次输入,做表头*/
else p1->next=p2; /*否则接到表尾*/
p2=p1;
p1=(struct ks *)malloc(LEN); /*开辟下一个空间*/
scanf("%ld%d",&p1->num,&p1->score);
p1->next=NULL;
}
free(p1);
return(head);
}
void output(*head) /*第二个错误,"Declaration syntax error."*/
{
struct ks *p;
p=head;
do
{
printf("%ld%-6d\n",p->num,p->score);
p=p->next;
}while(p!=NULL)
}
main()
{
struct ks &pe;
pe=create();
output(pe);
}
搜索更多相关主题的帖子: 链表 
2007-09-07 17:07
PcrazyC
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:5652
专家分:0
注 册:2006-10-20
收藏
得分:0 
应该不只两个.
我只改了编译错误,其它的我没有管

#define NULL 0
#define LEN sizeof(struct ks)
#include<stdio.h>
#include<stdlib.h>
int n;
struct ks /*结构体声明*/
{
long num;
int score;
struct ks *next;
}; //少了分号
struct ks *create()
{
struct ks *head,*p1,*p2;
n=0;head=p2=NULL;
p1=(struct ks *)malloc(LEN);
p1->next=NULL;
scanf("%ld%d",&p1->num,&p1->score);
while(p1->num!=0)
{
++n;
if(n==1)head=p1;
else p1->next=p2;
p2=p1;
p1=(struct ks *)malloc(LEN);
scanf("%ld%d",&p1->num,&p1->score);
p1->next=NULL;
}
free(p1);
return(head);
}
void output(struct ks *head) //变量要定义类型
{
struct ks *p;
p=head;
do
{
printf("%ld%-6d\n",p->num,p->score);
p=p->next;
}while(p!=NULL);//这句有问题,少了分号
}
int main()
{
struct ks *pe;//这句有问题
pe=create();
output(pe);
return 0;
}

雁无留踪之意,水无取影之心
2007-09-07 18:36
peswe
Rank: 1
等 级:新手上路
帖 子:197
专家分:0
注 册:2006-11-22
收藏
得分:0 

你的编程习惯有点不好哦!~ ^_^,建议你用vc学习!~
以下是我按你的程序修改之后的程序(红色的为我修改了的地方!~)

#include<stdio.h>
#include<stdlib.h>
#define NULL 0
int n;

struct ks /*结构体声明*/
{
long num;
int score;
struct ks *next;
};


struct ks *create() /*第一个错误,“Too many types in declaration."*/
/*这里我改后没出现错误提示,应该是你前面没定义好导致的错误!~*/
{
struct ks *head,*p1,*p2;
n=0;head=p2=NULL;
p1=(struct ks *)malloc(sizeof(struct ks)); /*为一个结构体开辟一个空间*/
p1->next=NULL;
printf("\nPlease input the first num and score:\t");
scanf("%ld,%d",&p1->num,&p1->score);
while(p1->num!=0)
{
++n;
if(n==1)head=p1; /*第一次输入,做表头*/
else p2->next=p1; /*否则接到表尾*//*这里是最严重的错误!~你对比一下看看!~*/
p2=p1;
p1=(struct ks *)malloc(sizeof(struct ks)); /*开辟下一个空间*/
printf("\nPlease input num and score:\t");/*在这里最好是用提示语!~*/
scanf("%ld,%d",&p1->num,&p1->score);/*(注意输入逗号!~)*/
p1->next=NULL;
}
free(p1);
return(head);
}


void output(struct ks *head) /*第二个错误,"Declaration syntax error."*//*这里你的*head没有类型,不错才怪*/
{
struct ks *p;
p=head;
do
{
printf("%ld\t%-6d\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}

int main()
{
struct ks *pe;/*这里你用取地址符&就更不通了!~*/
pe=create();
output(pe);
return 0;
}

[此贴子已经被作者于2007-9-10 22:02:31编辑过]


C斗士~~~fighting!!!!
2007-09-07 22:34
行走
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-7-26
收藏
得分:0 
谢谢上面两位,根据你们的修改,我改好后运行成功了!不过我还有个问题,用malloc函数开辟的空间会一直存在,要用free函数释放,上面的这个程序最后好象也没有用FREE函数啊,那运行时输入的数据会不会一直留在内存里呢?
2007-09-10 21:15
peswe
Rank: 1
等 级:新手上路
帖 子:197
专家分:0
注 册:2006-11-22
收藏
得分:0 
恩,会在的!
不过,到你关闭程序之后,电脑会自动释放内存的!~

[此贴子已经被作者于2007-9-10 21:59:29编辑过]


C斗士~~~fighting!!!!
2007-09-10 21:58
快速回复:[求助]链表,出了错,却总想不通为什么错了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013758 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved