#include
#include
using namespace std;
void Reverse_stack(stack& st, int n, int tmp)
{
if (n == 0)
return;
tmp = st.top();
st.pop();
Reverse_stack(st, n - 1, tmp);
st.push(tmp);
}
int main()
{
int n = 0;
cin >> n;
stackst;
int a;
for (int i = 0;i < n;i++)
{
cin >> a;
st.push(a);
}
int tmp = 0;
Reverse_stack(st, n, tmp);
for (int i = 0;i <n ;i++)
{
cout << st.top()<<" ";
st.pop();
}
return 0;
}
反转栈元素(递归算法实现)
最新推荐文章于 2022-11-25 01:07:00 发布