| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 735 人关注过本帖
标题:[求助]关于链表插入算法,编译通过,有2个警告,无法实现目的!
只看楼主 加入收藏
jiang520
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2006-9-13
收藏
 问题点数:0 回复次数:9 
[求助]关于链表插入算法,编译通过,有2个警告,无法实现目的!

#include <iostream>
#include <stdlib.h>
using namespace std;

struct stu
{
int num;
char name[20];
float score;
struct stu *next;
};

struct stu *creat(int n)
{
struct stu *head,*pf,*pb;
cout<<"输入结点数:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
pb=(struct stu*)malloc(sizeof(struct stu));
cout<<"请输入学生学号,姓名,成绩:"<<endl;
cin>>pb->num>>pb->name>>pb->score;
if(i==0)pf=head=pb;
else pf->next=pb;pb->next=NULL;
pf=pb;
}
return (head);
}

struct stu *insert(struct stu *head,struct stu *pi)
{
struct stu *pb,*pf;
pb=head;
if(head==NULL)
{
head=pi;
pi->next=NULL;
}
else
{
while((pi->num>pb->num)&&(pb->next!=NULL))
{
pf=pb;
pb=pb->next;
}
if(pi->num<=pb->num)
{
if(head=pb)head=pi;
else pf->next=pi;
pi->next=pb;
}
else
{
pb->next=pi;
pi->next=NULL;
}
}
return (head);
}

void display(struct stu* head)
{
cout<<"学生相关信息和成绩如下:"<<endl;
cout<<"学号\t姓名\t成绩:"<<endl;
while(head!=NULL)
{
cout<<head->num<<"\t"<<head->name<<"\t"<<head->score<<endl;
head=head->next;
}
}

int main()
{
struct stu *head,*p;
int k,num; //警告在这行和下一行
head=creat(k); //警告在这行和上一行
cin>>k;
display(head);
p=(struct stu *)malloc(sizeof(struct stu));
cin>>p->num>>p->name>>p->score;
head=insert(head,p);
display(head);
return 0;
}


各位帮小弟看看,在此先谢过!

[此贴子已经被作者于2006-11-23 15:23:57编辑过]

搜索更多相关主题的帖子: 目的 算法 链表 编译 警告 
2006-11-23 15:05
jiang520
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2006-9-13
收藏
得分:0 
哦,还有在当中用malloc申请内存后,忘了释放内存,
帮我顺便补上,呵呵!

努力,努力吧,未来的天空,那一片湛蓝总会属于我的~
2006-11-23 15:12
jiang520
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2006-9-13
收藏
得分:0 

#include <iostream>
#include <stdlib.h>
using namespace std;

struct stu
{
int num;
char name[20];
float score;
struct stu *next;
};

struct stu *creat(int n)
{
struct stu *head,*pf,*pb;
cout<<"输入结点数:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
pb=(struct stu*)malloc(sizeof(struct stu));
cout<<"请输入学生学号,姓名,成绩:"<<endl;
cin>>pb->num>>pb->name>>pb->score;
if(i==0)pf=head=pb;
else
{pf->next=pb;pb->next=NULL;
pf=pb;
}
}
return (head);
free(pb);
}

struct stu *insert(struct stu *head,struct stu *pi)
{
struct stu *pb,*pf;
pb=head;
if(head==NULL)
{
head=pi;
pi->next=NULL;
}
else
{
while((pi->num>pb->num)&&(pb->next!=NULL))
{
pf=pb;
pb=pb->next;
}
if(pi->num<=pb->num)
{
if(head=pb)head=pi;
else pf->next=pi;
pi->next=pb;
}
else
{
pb->next=pi;
pi->next=NULL;
}
}
return (head);
}

void display(struct stu* head)
{
cout<<"学生相关信息和成绩如下:"<<endl;
cout<<"学号\t姓名\t成绩:"<<endl;
while(head!=NULL)
{
cout<<head->num<<"\t"<<head->name<<"\t"<<head->score<<endl;
head=head->next;
}
}

int main()
{
struct stu *head,*p;
p=(struct stu *)malloc(sizeof(struct stu));
head=creat(0);
head=insert(head,p);
display(p);
free(p);
return 0;
}

乱修改了下,通过也没警告了,但这时不管怎么样输入,就是多了条乱码记录,不知道怎么回事
?


努力,努力吧,未来的天空,那一片湛蓝总会属于我的~
2006-11-23 16:04
comebaby
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2006-6-17
收藏
得分:0 

用编程器Dev-C++ 没问题

2006-11-23 16:53
jiang520
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2006-9-13
收藏
得分:0 

俺用TC,就这么糟糕啊?

努力,努力吧,未来的天空,那一片湛蓝总会属于我的~
2006-11-24 15:25
jiang520
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2006-9-13
收藏
得分:0 
哦,main()中的p分配了空间,需要赋值吗?
是不是多分配了一块内存而没有初始化,造成系统自动给你乱七八糟赋些值,
导致有那样的结果呢?
这里咋个修改啊?

努力,努力吧,未来的天空,那一片湛蓝总会属于我的~
2006-11-24 15:29
linghao
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2006-11-20
收藏
得分:0 
要的
初始化成NULL
2006-11-24 16:11
jiang520
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2006-9-13
收藏
得分:0 
汗,我知道啊,我既然在问,我就试过噻,
初始化成NULL,结果反而不出来了,
而在编译时竟也没有警告,在输完数据后,TempFile.exe说遇到问题要关闭,
而不初始化时,那就可以显示结果,只是多了一项乱码,

不知道咋弄了,希望有此经验的兄台,哥们帮我看看.
里面肯定还有问题,我也才学数据结构不久,有些东西可能不知道,或者没注意,
或者没理解,反正先谢谢了

努力,努力吧,未来的天空,那一片湛蓝总会属于我的~
2006-11-24 16:32
四处漂泊
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2005-11-28
收藏
得分:0 

#include <iostream>
#include <stdlib.h>
using namespace std;
struct stu
{
int num;
char name[20];
float score;
struct stu *next;
};

struct stu *creat(void)
{ int n;
struct stu *head=NULL,*pf,*pb;
cout<<"输入结点数:"<<endl;
cin>>n;
for(int i=0;i<n;i++)
{
pb=(struct stu*)malloc(sizeof(struct stu));
cout<<"请输入学生学号,姓名,成绩:"<<endl;
cin>>pb->num>>pb->name>>pb->score;
if(head==NULL)
pf=head=pb;
else
{pf->next=pb;
pf=pb;
}
if(pf) pf->next=NULL;
}
return (head);

}

struct stu *insert(struct stu *head)
{
struct stu *pb,*pf,*p;
pb=head;
cout<<"输入你要插入的结点"<<endl;
p=(struct stu *)malloc(sizeof(struct stu));
cin>>p->num>>p->name>>p->score;
if(head==NULL)
{
head=p;
p->next=NULL;
}
else
{
while((p->num>pb->num)&&(pb->next!=NULL))
{
pf=pb;
pb=pb->next;
}
p->next=pb;
pf->next=p;
}
return (head);
}

void display(struct stu* head)
{
cout<<"学生相关信息和成绩如下:"<<endl;
cout<<"学号\t姓名\t成绩:"<<endl;
while(head!=NULL)
{
cout<<head->num<<"\t"<<head->name<<"\t"<<head->score<<endl;
head=head->next;
}
}

int main()
{
struct stu *head;
head=creat();
head=insert(head);
display(head);
return 0;
}


万一链表没有排好序怎么插入,jiang520不仿再考虑下


2006-11-24 20:05
jiang520
Rank: 1
等 级:新手上路
帖 子:207
专家分:0
注 册:2006-9-13
收藏
得分:0 
嗯,谢谢楼上的了.我仔细看了看,我的算法思想基本上没有错误,
我想可能与编译器有关吧,我改了下,对比你的和书上的,可行了,但输入某些结点数就会出问题,
然后我把书上的,你的,分别帖到编译器里,都分别有这种问题,书上的说用VC6.0吧,我用TC,是不是可能呢?

我猜想是吧,还有四楼的兄台不是在DEV-C++中可以吧,我想是这样吧.

这个问题算了吧,没什么了,只要理解了算法思想就好了.谢谢各位!!

努力,努力吧,未来的天空,那一片湛蓝总会属于我的~
2006-11-25 08:43
快速回复:[求助]关于链表插入算法,编译通过,有2个警告,无法实现目的!
数据加载中...
 
   



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

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