| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 685 人关注过本帖
标题:动态链表问题
只看楼主 加入收藏
wyzn12
Rank: 1
等 级:新手上路
帖 子:129
专家分:0
注 册:2006-10-28
收藏
 问题点数:0 回复次数:1 
动态链表问题

下面的链表程序中若是把score的输出输入格式改为float就会报错:浮点语法错误。改成int就可以,不知道原因是什么?
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
int score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld,%d",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%d",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}

main()
{struct student *head;
void print(struct student *);
printf("\nInput data:\n");
head=creat();
print(head);
getch();
}
void print(struct student *head)
{struct student *p;
printf("\nNow ,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %5d\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}

搜索更多相关主题的帖子: include 链表程序 动态 
2006-10-29 19:19
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

/*看看这样可以不.将下面的folat改成double会更好点.还有建议在输入格式中不要用逗号隔开.*/
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *)malloc(LEN);
scanf("%ld%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}

main()
{struct student *head;
void print(struct student *);
printf("\nInput data:\n");
head=creat();
print(head);
getch();
}
void print(struct student *head)
{struct student *p;
printf("\nNow ,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %5f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}


倚天照海花无数,流水高山心自知。
2006-10-29 20:56
快速回复:动态链表问题
数据加载中...
 
   



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

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