| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 445 人关注过本帖
标题:请教结构习题遇到的问题
只看楼主 加入收藏
handsome303
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-2-28
收藏
 问题点数:0 回复次数:5 
请教结构习题遇到的问题
题目是将一个原链表逆置,重新制作成一个逆置链表

我的程序能通过编译,不过途中提示遇到错误,强制关闭。
望各位大虾帮帮忙,指点迷津!

#include<iostream.h>

struct part
{
long num;
part *next;
};

part *head;

part *original()
{
cout<<"请输入原链表的号码:"<<endl;
part *ps;
ps=new part;
cin>>ps->num;
part *end;
end=ps;
head=ps;
for(int i=0;i<3;i++)
{
ps=new part;
cin>>ps->num;
end->next=ps;
end=ps;
}
end->next=NULL;
delete ps;
return (head);
} //原链表的函数

void oppsite(part *dest) //逆置表的函数
{
cout<<"head-->";
while(dest)
{
cout<<dest->num<<"-->";
dest =dest->next;
}
cout<<"NULL"<<endl; //先打印原链表,dest的值回到空值

part *head1; //新建逆置链表的链首head1
head1=dest;
part *ps1;
ps1=dest;
ps1->next=dest-8; //一个dest的大小为8,从空值返回到上一个值(这里不太肯定,不知道对不对)
while(dest)
{
ps1=dest;
dest=dest-8;
ps1->next=dest-8;
}
ps1->next=NULL; //制逆置表完毕

cout<<"head-->";
while(head1)
{
cout<<head1->num<<"-->";
head1=head1->next;
}
cout<<"NULL"<<endl;
} //打印逆置链表

void main()
{
oppsite(original());
}





搜索更多相关主题的帖子: 习题 结构 
2006-05-29 16:06
cathypu0725
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2006-5-26
收藏
得分:0 
part *original()
{
cout<<"请输入原链表的号码:"<<endl;
part *ps;
ps=new part;
cin>>ps->num;
part *end;
end=ps;
head=ps;
for(int i=0;i<3;i++)
{
ps=new part;
cin>>ps->num;
end->next=ps;
end=ps;
}
end->next=NULL;
delete ps;
return (head);
} //原链表的函数
红色标注的这句,应该有问题的,不明白你为什么要删除ps???
2006-05-29 16:54
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 

#include<iostream.h>

struct part
{
long num;
part *next;
part *last;
};

part *head;

part *original()
{
cout<<"请输入原链表的号码:"<<endl;
part *ps;
ps=new part;
ps->last=NULL;
cin>>ps->num;
part *end;

end=ps;
head=ps;
for(int i=0;i<3;i++)
{
ps=new part;
cin>>ps->num;
end->next=ps;
ps->last=end;
end=ps;
}
end->next=NULL;
return (head);
} //原链表的函数

void oppsite(part *dest) //逆置表的函数
{
cout<<"head-->"<<dest->num;
while(dest->next)
{
cout<<dest->num<<"-->";
dest =dest->next;
}
cout<<"NULL"<<endl; //先打印原链表,dest的值回到空值

part *head1; //新建逆置链表的链首head1
head1=dest;
part *ps1;
ps1=dest;

cout<<"head-->";
while(head1)
{
cout<<head1->num<<"-->";
head1=head1->last;
}
cout<<"NULL"<<endl;
} //打印逆置链表

void main()
{
oppsite(original());
}


嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-05-29 17:08
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
以下是引用song4在2006-5-29 17:08:00的发言:

#include<iostream.h>

struct part
{
long num;
part *next;
part *last;//双向链表
};

part *head;

part *original()
{
cout<<"请输入原链表的号码:"<<endl;
part *ps;
ps=new part;
ps->last=NULL;
cin>>ps->num;
part *end;

end=ps;
head=ps;
for(int i=0;i<3;i++)
{
ps=new part;
cin>>ps->num;
end->next=ps;
ps->last=end;
end=ps;
}//delete掉了PS;而PS就在链表里COUT<<PS;就会出错;

end->next=NULL;
return (head);
} //原链表的函数

void oppsite(part *dest) //逆置表的函数
{
cout<<"head-->"<<dest->num;
while(dest->next)
{
cout<<dest->num<<"-->";
dest =dest->next;
}//这才真正使dest是最后一个
cout<<"NULL"<<endl; //先打印原链表,dest的值回到空值

part *head1; //新建逆置链表的链首head1
head1=dest;//要不这里head1是空的
part *ps1;
ps1=dest;

cout<<"head-->";
while(head1)
{
cout<<head1->num<<"-->";
head1=head1->last;
}
cout<<"NULL"<<endl;
} //打印逆置链表

void main()
{
oppsite(original());
}
//楼主这里是new出来的结构,根本不是连续的.


嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-05-29 17:13
handsome303
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-2-28
收藏
得分:0 
谢谢二楼的大大和版主,又学到new知识了,不过有点不明白,ps作为结点,到最后不是要用delete释放内存空间,而end不是将整条链连起来吗?

2006-05-29 19:14
song4
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:38
帖 子:1533
专家分:4
注 册:2006-3-25
收藏
得分:0 
到最后
dest与end全指最后一个呢
他们指一个空间
这个给释放了
那个也没了

嵌入式 ARM 单片机 驱动 RT操作系统 J2ME LINUX  Symbian C C++ 数据结构 JAVA Oracle 设计模式 软件工程 JSP
2006-05-29 19:28
快速回复:请教结构习题遇到的问题
数据加载中...
 
   



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

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