#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;
}
以上解法用到了两个递归,第一次出栈遍历时用到了递归,第二次入栈操作时又用到递归