64,639
社区成员
发帖
与我相关
我的任务
分享
#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
#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;
}
#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();
}
}