| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 460 人关注过本帖
标题:[求助]单链表错哪了??
只看楼主 加入收藏
起点线
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2006-12-7
收藏
 问题点数:0 回复次数:2 
[求助]单链表错哪了??

输入6学生的数据为什么输出时只有5个的?

格式:
姓名 学号 成绩1 成绩2
...
...


#include <stdio.h>
#include <stdlib.h>
#define N struct student
#define S sizeof(N)

struct student
{
char name[15];
int num;
float yuwen;
float math;
N *next;
};

N *hilo(N *head);
N *find(N *p1,N *head);

void main()
{
int i,n=0;
int num;
N *p1,*p2,*head;
p1=p2=(N*)malloc(S);
scanf("%s",p1->name);
getchar();
scanf("%d",&p1->num);
getchar();
scanf("%f",&p1->yuwen);
getchar();
scanf("%f",&p1->math);
head=NULL;

for(i=1;i<6;i++)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(N*)malloc(S);
scanf("%s",p1->name);
getchar();
scanf("%d",&p1->num);
getchar();
scanf("%f",&p1->yuwen);
getchar();
scanf("%f",&p1->math);
}
p2->next=0;
p1=head;

while(p1!=0)
{
printf("%s %d %0.1f %0.1f\n",p1->name,p1->num,p1->yuwen,p1->math);
p1=p1->next;
}
}

搜索更多相关主题的帖子: 单链 
2007-06-03 16:10
chyhome
Rank: 1
等 级:新手上路
威 望:1
帖 子:102
专家分:0
注 册:2007-5-13
收藏
得分:0 

最后一次输入没有链接上就退出了循环。其实第一次初始输入就可以直接让head=p1;后面循环就不用IF判断了,然后把链表设置语句移到后面。
[QUOTE] head=NULL;

for(i=1;i<6;i++)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(N*)malloc(S);
scanf("%s",p1->name);
getchar();
scanf("%d",&p1->num);
getchar();
scanf("%f",&p1->yuwen);
getchar();
scanf("%f",&p1->math);
}[/QUOTE]
改为如下:
head=p1;

for(i=1;i<6;i++)
{
n++;
p1=(N*)malloc(S);
scanf("%s",p1->name);
getchar();
scanf("%d",&p1->num);
getchar();
scanf("%f",&p1->yuwen);
getchar();
scanf("%f",&p1->math);
p2->next=p1;
p2=p1;

}


[此贴子已经被作者于2007-6-3 16:41:46编辑过]


2007-06-03 16:41
起点线
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2006-12-7
收藏
得分:0 

谢谢了,,烦了我一下午了,终于解决了..


2007-06-03 17:49
快速回复:[求助]单链表错哪了??
数据加载中...
 
   



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

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