找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 511|回复: 9

[求助]关于链表插入算法,编译通过,有2个警告,无法实现目的!

[复制链接]
发表于 2009-11-4 01:19:11 | 显示全部楼层 |阅读模式 IP:江苏扬州
#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;
}


各位帮小弟看看,在此先谢过!
发表于 2009-11-4 01:19:12 | 显示全部楼层 IP:江苏扬州
哦,还有在当中用malloc申请内存后,忘了释放内存,
帮我顺便补上,呵呵!
回复

使用道具 举报

发表于 2009-11-4 01:19:13 | 显示全部楼层 IP:江苏扬州
#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;
}

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

使用道具 举报

发表于 2009-11-4 01:19:14 | 显示全部楼层 IP:江苏扬州
用编程器Dev-C++ 没问题
回复

使用道具 举报

发表于 2009-11-4 01:19:16 | 显示全部楼层 IP:江苏扬州

俺用TC,就这么糟糕啊?
回复

使用道具 举报

发表于 2009-11-4 01:19:17 | 显示全部楼层 IP:江苏扬州
哦,main()中的p分配了空间,需要赋值吗?
是不是多分配了一块内存而没有初始化,造成系统自动给你乱七八糟赋些值,
导致有那样的结果呢?
这里咋个修改啊?
回复

使用道具 举报

发表于 2009-11-4 01:19:18 | 显示全部楼层 IP:江苏扬州
要的
初始化成NULL
回复

使用道具 举报

发表于 2009-11-4 01:19:19 | 显示全部楼层 IP:江苏扬州
汗,我知道啊,我既然在问,我就试过噻,
初始化成NULL,结果反而不出来了,
而在编译时竟也没有警告,在输完数据后,TempFile.exe说遇到问题要关闭,
而不初始化时,那就可以显示结果,只是多了一项乱码,

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

使用道具 举报

发表于 2009-11-4 01:19:21 | 显示全部楼层 IP:江苏扬州
#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不仿再考虑下
回复

使用道具 举报

发表于 2009-11-4 01:19:22 | 显示全部楼层 IP:江苏扬州
嗯,谢谢楼上的了.我仔细看了看,我的算法思想基本上没有错误,
我想可能与编译器有关吧,我改了下,对比你的和书上的,可行了,但输入某些结点数就会出问题,
然后我把书上的,你的,分别帖到编译器里,都分别有这种问题,书上的说用VC6.0吧,我用TC,是不是可能呢?

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

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

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 01:23 , Processed in 0.272653 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表