链表小问题,麻烦各位

wwoo_1105 2008-03-04 02:53:46
下面一段代码是一个链表实例,不过在链表输出的时候总是最后一个元素打印不出来,各位老鸟帮忙看看吧。
#include <stdio.h>
#include <malloc.h>
#include <iostream.h>
typedef struct node
{
char name[20];
struct node *link;
}stud;
stud *create(int n)
{
stud *p,*h,*s;
h=(stud*)malloc(sizeof(stud));
//h->name[0]='\0';
h->link=NULL;
p=h;
for (int i=0;i<n;i++)
{
s=(stud*)malloc(sizeof(stud));
cout<<"Please input student's name:"<<endl;
cin>>s->name;
p->link=s;
p=s;
}
p->name[0]='\0';
return h;
}

bool output(stud *head)
{
stud *p;
p=head->link;
while(1)
{
if('\0'==p->name[0])
break;
cout<<p->name<<endl;
p=p->link;
}
return 1;
}

void main()
{
int number;
cout<<"Please input student's number:"<<endl;
cin>>number;
stud *head=create(number);
output(head);
}
...全文
158 15 打赏 收藏 举报
写回复
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
appley 2008-03-07
  • 打赏
  • 举报
回复
mark
herman~~ 2008-03-04
  • 打赏
  • 举报
回复
mark
dskgo 2008-03-04
  • 打赏
  • 举报
回复
我觉得楼主的while(p)这一句写成:while(P != NULL)较好,因为p是一个指针.
如果p是布尔变量,即可用while(p);
还有如果P是double变量......
读林税的<高质量程序设计>有感.
wwoo_1105 2008-03-04
  • 打赏
  • 举报
回复
嗯,确实,我就是C和C++不分,头痛呀
楼上建议很好,学习了,谢谢
hai040 2008-03-04
  • 打赏
  • 举报
回复
c++?c?
typedef struct node
{
char name[20];
struct node *link;
}stud;
这是典型的c风格
另外,最好用类来组织代码(如果还没学到就算了)
对bool,c++有两个关键词true/false,不要返回1
void main()好像连c的新标准都不支持,用int main()
还有尽量不要用一个字符做变量名,用有意义的英文(缩写)
dskgo 2008-03-04
  • 打赏
  • 举报
回复
最近在复习数据结构,根据6楼的意见把楼主的代码改了一下,不妥之处请大家指正.^_^
#include <stdio.h>
#include <malloc.h>
#include <iostream.h>
typedef struct node
{
char name[20];
struct node *link;
}stud;

class Link
{
public:
Link(int n);
~Link();
void output();
private:
stud *head;
};

Link::Link(int n)
{
head = (stud*)malloc(sizeof(stud));
head->link = NULL;
stud *s,*p;
p = head;
for(int i=0; i<n; i++)
{
s = (stud*)malloc(sizeof(stud));
cout<<"Please input student's name:"<<endl;
cin>>s->name;
p->link = s;
p = s;
}
p->link = NULL;
}

Link::~Link()
{
stud *p;
while(head != NULL)
{
p = head;
head = head->link;
free(p);
}
}

void Link:: output()
{
stud *p;
p = head->link;
while(p != NULL)
{
cout<<p->name<<endl;
p = p->link;
}
}

void main()
{
int number;
cout<<"Please input student's number:"<<endl;
cin>>number;
Link students(number);
students.output();
}
天亮后说晚安 2008-03-04
  • 打赏
  • 举报
回复
链表我也 头疼!!!!
学习 一下
支持楼主!
Chappell 2008-03-04
  • 打赏
  • 举报
回复
lz不错……
wwoo_1105 2008-03-04
  • 打赏
  • 举报
回复
帮我看看这样行不行:
void strdestroy(stud *head)
{
stud *p,*s;
p=head;
s=p;
while(p)
{
p=p->link;
free(s);
s=p;
}
}
baihacker 2008-03-04
  • 打赏
  • 举报
回复
一个最大的问题就是分配的内存没有回收...
自己写一个destroy函数...
wwoo_1105 2008-03-04
  • 打赏
  • 举报
回复
哦,明白了,谢谢楼上二位
另外这段代码写的有什么不妥的吗?
任何毛病都行,我初学,不想养成坏习惯^~^
baihacker 2008-03-04
  • 打赏
  • 举报
回复
create中
p->link = 0;
return h;
}
output中:
while(p)
{
cout<<p->name<<endl;
p=p->link;
}
wwoo_1105 2008-03-04
  • 打赏
  • 举报
回复
哪里?能不能详细点呢
baihacker 2008-03-04
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <malloc.h>
#include <iostream.h>
typedef struct node
{
char name[20];
struct node *link;
}stud;

stud *create(int n)
{
stud *p,*h,*s;
h=(stud*)malloc(sizeof(stud));
//h->name[0]='\0';
h->link=NULL;
p=h;
for (int i=0;i<n;i++)
{
s=(stud*)malloc(sizeof(stud));
cout<<"Please input student's name:"<<endl;
cin>>s->name;
p->link=s;
p=s;
}
p->link = 0;
return h;
}

bool output(stud *head)
{
stud *p;
p=head->link;
while(p)
{
cout<<p->name<<endl;
p=p->link;
}
return 1;
}

void main()
{
int number;
cout<<"Please input student's number:"<<endl;
cin>>number;
stud *head=create(number);
output(head);
}

lala_benben 2008-03-04
  • 打赏
  • 举报
回复
Create 方法有问题。。
相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-03-04 02:53
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下