一道关于栈的算法题,大家帮忙哈

rock_HX 2008-11-02 01:59:57
在数据结构与算法----c++版上.
题目很短:

将栈s中的元素的顺序倒过来.限制:只使用一个附加的栈和几个附加的非数组变量
...全文
165 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
rock_HX 2008-11-02
  • 打赏
  • 举报
回复
谢谢各位了..结贴去
aFlyingEagle 2008-11-02
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include <iostream>
#include <stack>
using namespace std;

const int MAXSIZE = 100;
int main()
{
int a[MAXSIZE]; //创建数组
int a_size;
int e;
cout<<"请输入所要创建的数组的大小: ";
cin>>a_size;
cout<<"请输入数组中的元素:"<<endl;
for(int i=0; i<a_size; i++)
{
cin>>e;
a[i] = e;
}

stack<int> s1,s2;
int tmp;
for(int k=0; k<a_size; k++) //初始化栈
s1.push(a[k]);
int count=a_size;
while(count != 0) //转换操作
{
tmp = s1.top();
s1.pop();
for(int g=0; g<count-1; g++)
{
s2.push(s1.top());
s1.pop();
}
s1.push(tmp);
for(int h=0; h<count-1; h++)
{
s1.push(s2.top());
s2.pop();
}
count--;
}
cout<<"改变顺序后"<<endl;
for(int j=0;j<a_size;j++) //显示结果
{
cout<<s1.top()<<" ";
s1.pop();
}
return 0;
}
kkndciapp 2008-11-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 rock_HX 的回复:]
kkndciapp:
谢谢先,但是书上要求原来栈里的顺序要倒过来..不是说倒过来读取,要把原始资源也改变顺序才行的
我也想了很久的..是在做不出来..这个题可不是看上去这样简单..呵呵帮忙多想想吧
[/Quote]
按照你要求全部过程都在同一个栈里面完成,把原始资源倒序
kkndciapp 2008-11-02
  • 打赏
  • 举报
回复
不用谢,开始没有理解你的意思,现在知道了,帮你写了一个
#include<iostream>
#include<stack>
using namespace std;

void main()
{
int a[6]={1,2,3,4,5,6};
int size=6;//数组大小,自己可以配置
stack<int> s,s_tmp;
for(int ii=0;ii<size;ii++)
s.push(a[ii]);
//这里的s栈的栈顶是6,栈低是1
int i=size,temp,count=0;
while(i!=1)
{
for(int k=0;k<size-1;k++)
{
s_tmp.push(s.top());
s.pop();
}
temp=s.top();
s.pop();
for(int w=0;w<i-1;w++)
{
s.push(s_tmp.top());
s_tmp.pop();
}
s.push(temp);
while(!s_tmp.empty())
{
s.push(s_tmp.top());
s_tmp.pop();
}
i--;
}
while(!s.empty())
{
cout<<s.top();//倒完序后,可以看到,栈顶变成1,栈低为6
s.pop();
}
}

123456Press any key to continue
Jarrys 2008-11-02
  • 打赏
  • 举报
回复
#include<iostream>
#include<stack>

using namespace std;

int main()
{
int Array[]={1,2,3,4,5,6,7,8,9}; //初始化数组,也作临时数组
int Num=0; //循环变量
stack<int> Stack;

for(int i=0;i<sizeof Array/sizeof Array[0];++i) //初始化栈
Stack.push(Array[i]);
while(!Stack.empty()) //将栈中的元素再一次弹到数组中,在数组中顺序就反过来了
{
Array[Num++]=Stack.top();
cout<<Stack.top()<<" ";
Stack.pop();
}
cout<<endl;
for(i=0;i<sizeof Array/sizeof Array[0];++i) //将临时数组中数据再压入栈中
Stack.push(Array[i]);
while(!Stack.empty())
{
cout<<Stack.top()<<" ";
Stack.pop();
}
cout<<endl;
return 0;
}

帅得不敢出门 2008-11-02
  • 打赏
  • 举报
回复
只使用一个附加的栈和几个附加的非数组变量
可以实现的 不过我这个方法很严重浪费时间啊 哈哈
画在纸上很明白 可以没办法贴到这里
就打字吧 晕死
原栈是sa内容为12345 栈顶元素是1现在要变成54321 额外栈为sb一个额外变量是tmp
第一次 依次出栈 最后一个放入tmp tmp =5 sb = null 4 3 2 1 tmp入 sb sb = 51234
sb出栈 push到sa sa = 4 3 2 1 5
第二次 sa出栈 最后一个给tmp tmp = 4; sb = null 5 1 2 3 再push tmp sb = 45123
sb 出栈push入sa sa = 32154 原理同上 .... .. sb = 34512
.............
最后 sb = 12345
最最后一步sb出栈入sa sa = 54321
rock_HX 2008-11-02
  • 打赏
  • 举报
回复
kkndciapp:
谢谢先,但是书上要求原来栈里的顺序要倒过来..不是说倒过来读取,要把原始资源也改变顺序才行的
我也想了很久的..是在做不出来..这个题可不是看上去这样简单..呵呵帮忙多想想吧
kkndciapp 2008-11-02
  • 打赏
  • 举报
回复
#include<iostream>
#include<stack>
using namespace std;

void main()
{
int a[4]={1,2,3,4};
stack<int> s,s_temp;
for(int k=0;k<4;k++)
s.push(a[k]);//enter : 1,2,3,4
while(!s.empty())
{
s_temp.push(s.top());//enter s_temp: 4,3,2,1
s.pop();
}
while(!s_temp.empty())
{
cout<<s_temp.top();//pop
s_temp.pop();
}
}
rock_HX 2008-11-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hqin6 的回复:]
晕,弹出来,到另一个栈t,然后将t复制到s不就得了么?
[/Quote]
不允许用复制的...书上的栈没提供这个函数..不然这个题目还有什么意思

[Quote=引用 2 楼回复:]
用两个栈,先进入一个,然后出栈到第二个栈,然后第二个出栈进去第一个,完成倒序
[/Quote]
要按照限制的...
kkndciapp 2008-11-02
  • 打赏
  • 举报
回复
用两个栈,先进入一个,然后出栈到第二个栈,然后第二个出栈进去第一个,完成倒序
太乙 2008-11-02
  • 打赏
  • 举报
回复
晕,弹出来,到另一个栈t,然后将t复制到s不就得了么?

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧