| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 480 人关注过本帖
标题:[求助]链表没有输出来~
只看楼主 加入收藏
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
 问题点数:0 回复次数:4 
[求助]链表没有输出来~

struct student
{
long number;
float score;
student * next;
};

student * head; //链首指针

student * creat() //创建链表
{
student *ps; //结点指针
student *pend; //链尾指针
ps=new student; //分配内存空间,建立一个节点,准被插入链表
cin>>ps->number>>ps->score;
head=NULL; //开始链表为空
pend=ps;

while(ps->number!=0)
{
if(head==NULL)
pend=ps;
else
pend->next=ps;

pend=ps;
ps=new student;
cin>>ps->number>>ps->score;
}
pend->next=NULL;
delete ps;
return head;

}

void showlist(student * head)
{
cout<<"now the items of list are:"<<endl;
while(head)
{
cout<<head->number<<","<<head->score<<endl;
head=head->next;
}
}

void main()
{
showlist (creat());
}

输入后,显示now the items of list are: 下面就不显示了,输出有什么错误??

[此贴子已经被作者于2007-10-23 11:09:30编辑过]

搜索更多相关主题的帖子: 链表 
2007-10-23 10:38
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
建表错误

Fight  to win  or  die...
2007-10-23 11:23
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

while(ps->number!=0)
{
if(head==NULL)
pend=ps;
else
pend->next=ps;

pend=ps;
ps=new student;
cin>>ps->number>>ps->score;
}
整个建立链表都没有和head挂钩,你返回这个做什么.
pend->next=NULL;
delete ps;
return head;

喜欢的话可以去数据结构去看我的链表帖子


倚天照海花无数,流水高山心自知。
2007-10-23 11:48
绝地天使
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-22
收藏
得分:0 

#include <iostream.h>
struct student
{
long number;
float score;
student * next;
};
int n;

student * creat()
{ student * head;
student *ps;
student *pend;
n=0 ;
pend=ps=new student;
cin>>ps->number>>ps->score;
head=NULL;

while(ps->number!=0)
{
n=n+1;
if(n==1) head=ps;
else
pend->next=ps;

pend=ps;
ps=new student;
cin>>ps->number>>ps->score;
}
pend->next=NULL;
return head;

}

void showlist(student * head)
{ struct student *p;
p=head;
if(head!=NULL)
cout<<"now the items of list are:"<<endl;
while(head)
{
cout<<head->number<<","<<head->score<<endl;
head=head->next;
}
}

void main()
{
showlist (creat());
}


我的世界不充许你不存在!!!!!1111
2007-10-23 12:05
六道
Rank: 1
等 级:新手上路
帖 子:120
专家分:0
注 册:2007-9-28
收藏
得分:0 

知道到了,谢谢~~

//链表的建立,删除,插入
struct student
{
long number;
float score;
student * next;
};

student * head; //链首指针
student *stud; //插入节点指针

student * creat() //创建链表
{
student *ps; //结点指针
student *pend; //链尾指针
ps=new student; //分配内存空间,建立一个节点,准被插入链表
cin>>ps->number>>ps->score;
head=NULL; //开始链表为空
pend=ps;

while(ps->number!=0) //输入为0结束
{
if(head==NULL)
head=ps;
else
pend->next=ps;

pend=ps;
ps=new student;
cin>>ps->number>>ps->score;
}
pend->next=NULL;
delete ps;
return head;

}

void showlist(student * head) //打印链表信息
{
cout<<"\nnow the items of list are:"<<endl;
while(head)
{
cout<<head->number<<","<<head->score<<endl;
head=head->next;
}
}

void ldelete(student *head,long number) //删除链表节点
{
char ch;
cout<<"是否要删除?(Y/N)";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"enter a number will be deleted:";
cin>>number;
student *p;
if(!head)
{
cout<<"list is null.";
return; //未作删除,返回
}

if(head->number==number) //要删除的节点在链首
{
p=head;
head=head->next;
delete p;
cout<<number<<"the head of list have been deleted.\n\n";
return;
}

for(student *pguard=head;pguard->next;pguard=pguard->next) //删除的节点在链中或结尾
{
if(pguard->next->number==number)
{
p=pguard->next;
pguard->next=p->next;
delete p;
cout<<number<<"have been deleted.\n\n";
return;
}
}
cout<<number<<"not found!\n"; //此处表示未找到要删除的节点
}
}

void insert(student *head,student *stud) //插入链表节点
{
char ch;

cout<<"\n是否要插入?(Y/N)";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cout<<"enter informaion of insert:";
cin>>stud->number>>stud->score;
if(head==NULL)
{
head=stud;
stud->next=NULL;
return;
}

if(head->number>stud->number) //插入节点的位置在链首
{
stud->next=head;
stud=head; //插入节点成为链首
return;
}

student * pguard=head; //把插入节点先放置到链首
while((pguard->next)&&(pguard->next->number)<(stud->number))//未找到,继续向后走
pguard=pguard->next;

stud->next=pguard->next; //找到插入位置进行操作
pguard->next=stud;
}
}

void main()
{
long number;
student st;
showlist(creat());
ldelete(head,number);
showlist(head);
insert(head,&st);
showlist(head);
}

想以学号大小来排序,算法没想出来~


★孤独的人是可耻的★
2007-10-23 13:35
快速回复:[求助]链表没有输出来~
数据加载中...
 
   



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

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