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

#include<stdio.h>
#define LEN sizeof(struct student)
struct student
{int data;
struct student *next;
};


struct student *creat(struct student *head)
{struct student *p1,*newp;
head=(struct student *)malloc(LEN);
p1=(struct student *)malloc(LEN);
scanf("%d",&p1->data);
head=p1;
p1->next=NULL;
newp=(struct student*)malloc(LEN) ;
while(1)
{printf("please input the data");
scanf("%d",&newp->data);
if(newp->data==0)
break;
newp->next=NULL;
p1->next=newp;
p1=newp;

newp=(struct student*)malloc(LEN) ;
}
return head;
}
void Print(struct student *head)
{struct student *p;
p=head;
while(p->next!=NULL)
printf("%d\n",p->data);
p=p->next; }
main()
{struct student *head;
creat(head);
Print(head);
}
虽然通过了编译可是输出结果不对,一直输出很多数字,难道是跳不出while()循环

忘不吝赐教!

搜索更多相关主题的帖子: 链表 student struct 动态 LEN 
2006-08-08 16:49
soft_wind
Rank: 3Rank: 3
等 级:新手上路
威 望:8
帖 子:1430
专家分:0
注 册:2006-4-5
收藏
得分:0 

#include<stdio.h>
#define LEN sizeof(struct student)
struct student
{
int data ;
struct student*next ;
};


struct student*creat(struct student*head)
{
struct student*p1,*newp ;
head=(struct student*)malloc(LEN);
scanf("%d",&head->data);
head->next=NULL ;
p1=head;
while(1)
{
newp=(struct student*)malloc(LEN);
printf("please input the data");
scanf("%d",&newp->data);
if(newp->data==0)
break ;
newp->next=NULL ;
p1->next=newp ;
p1=newp ;

}
return head ;
}
void Print(struct student*head)
{
struct student *p ;
p=head ;
while(p)
{
printf("%d\n",p->data);
p=p->next ;
}
}
void Free(struct student *head)
{
struct student *p;
while(head)
{
p=head;
head=head->next;
free(p);
}
}
main()
{
struct student*head=NULL ;
Print(creat(head));
Free(head);
getch();
}



错误1:重复申请空间给head;
错误2:节点申请的空间没有释放;
警告:节点的连接显得很麻烦。


对不礼貌的女生收钱......
2006-08-08 20:09
横眉冷对
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2005-3-3
收藏
得分:0 
以下是引用soft_wind在2006-8-8 20:09:20的发言:

#include<stdio.h>
#define LEN sizeof(struct student)
struct student
{
int data ;
struct student*next ;
};


struct student*creat(struct student*head)
{
struct student*p1,*newp ;
head=(struct student*)malloc(LEN);
scanf("%d",&head->data);
head->next=NULL ;
p1=head;
while(1)
{
newp=(struct student*)malloc(LEN);
printf("please input the data");
scanf("%d",&newp->data);
if(newp->data==0)
break ;
newp->next=NULL ;
p1->next=newp ;
p1=newp ;

}
return head ;
}
void Print(struct student*head)
{
struct student *p ;
p=head ;
while(p)
{
printf("%d\n",p->data);
p=p->next ;
}
}
void Free(struct student *head)
{
struct student *p;
while(head)
{
p=head;
head=head->next;
free(p);
}
}
main()
{
struct student*head=NULL ;
Print(creat(head));
Free(head);
getch();
}



错误1:重复申请空间给head;
错误2:节点申请的空间没有释放;
警告:节点的连接显得很麻烦。

谢谢!
受教了!


MSN:jyycom@ 爱好:军事,体育,Rock。
2006-08-08 21:20
横眉冷对
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2005-3-3
收藏
得分:0 

有的书上head只是一个指向头节点的指针
而有的书head却直接是头节点。
做链表的程序总是在生成的时候出错。
不知道哪种思想简单一些


MSN:jyycom@ 爱好:军事,体育,Rock。
2006-08-08 22:39
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
head直接做头结点,但它只有空间,没有实际的意义,即它不参加遍历,只做链表的首地址.

倚天照海花无数,流水高山心自知。
2006-08-08 23:57
横眉冷对
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2005-3-3
收藏
得分:0 

在两位的指点下终于程序终于能运行了。


MSN:jyycom@ 爱好:军事,体育,Rock。
2006-08-09 14:22
快速回复:求助,动态链表的问题
数据加载中...
 
   



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

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