关于数组的逆置问题

来源:互联网 发布:黑帽seo 编辑:程序博客网 时间:2024/05/17 08:57
#include<iostream>
#include<stack>
using namespace std;
void addtostack(stack<int>&Stack,top)
{
  if(Stack.empty())
{
  push(top);

}
else
{
  p=stack.top();
stack.pop();
addtostack(Stack,top);
stack.push(p);
}
}
void Reverse(stack<int>&Stack)
{
  while(!Stack.empty())
  {int top=Stack.top();
   Stack.pop();
   Reverse(Stack);
   addtostack(Stack,top);  
}
}
int main()
{
   stack<int>&Stack;
   for(int I=0;i<5;i++)
   {
    Stack.push(i);
 
}
Reverse(Stack);
while(!stack.empty())
{
   cout<<Stack.top()<<endl;
   Stack.pop();

}
return 0;

}
以上解法用到了两个递归,第一次出栈遍历时用到了递归,第二次入栈操作时又用到递归
0 0
原创粉丝点击