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

下面的这个链表为什么老是不对呢,哪个大虾能帮帮我行吗
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct stud)
struct stud
{
int num;
float scores;
struct stud *next;
};
main()
{
struct stud *head,*p1,*p2;
int n=0;
p1=p2=(struct stud *)malloc(LEN);
scanf("num=%d,scores=%f",&p1->num,&p1->scores);
head=NULL;
while(p1->num!=0)
{
n++;
if (n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct stud *)malloc(LEN);
scanf("num=%d,scores=%f",&p1->num,&p1->scores);
}
p2->next=NULL;
p2=head;
while(p2->next!=NULL)
{
printf("num=%d scores=%f",p2->num,p2->scores);
p2=p2->next;
}

getch();
}

搜索更多相关主题的帖子: 链表 
2006-04-06 14:02
SunShining
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:31
帖 子:2215
专家分:0
注 册:2006-2-17
收藏
得分:0 
以下是引用Pallove在2006-4-6 14:02:00的发言:

下面的这个链表为什么老是不对呢,哪个大虾能帮帮我行吗
#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct stud)
struct stud
{
int num;
float scores;
struct stud *next;
};
main()
{
struct stud *head,*p1,*p2;
int n=0;
float *a,b;
a=&b ; /* 这里的借花献佛的..相关的请看 http://bbs.bc-cn.net/bbs/dispbbs.asp?boardID=5&ID=55506&page=1 如果不是TC则不用 */

p1=p2=(struct stud *)malloc(LEN);
scanf("%d%f",&p1->num,&p1->scores); /* 注意这的 scanf() 希望你不是马虎这里啦 */
head=NULL;
while(p1->num!=0)
{
n++;
if (n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct stud *)malloc(LEN);
scanf("%d%f",&p1->num,&p1->scores); /* 这里也是 */
}
p2->next=NULL;
p2=head;
while(p2!=NULL) /* 这应该是 p2!=NULL 如果象你那样.能打印出最后一个吗? */
{
printf("num=%d scores=%.2f ",p2->num,p2->scores);
p2=p2->next;
}

getch();
}


[glow=255,violet,2]闭关修炼ing...[/glow] [FLASH=360,180]http://www./chinaren.swf[/FLASH]
2006-04-06 15:10
快速回复:链表确实有很多问题
数据加载中...
 
   



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

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