Data Structure Stack: Reverse a stack using recursion
2024-09-07 19:01:07
http://www.geeksforgeeks.org/reverse-a-stack-using-recursion/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
#include <set>
using namespace std; void insertbottom(stack<int> &S, int top) {
if (S.empty()) S.push(top);
else {
int tmp = S.top();
S.pop();
insertbottom(S, top);
S.push(tmp);
}
} void reversestack(stack<int> &S) {
if (S.empty()) return;
int top = S.top();
S.pop();
reversestack(S);
insertbottom(S, top);
} int main() {
stack<int> S;
S.push();
S.push();
S.push();
S.push();
reversestack(S);
while (!S.empty()) {
cout << S.top() << endl;
S.pop();
}
return ;
}
最新文章
- [C#] 走进异步编程的世界 - 剖析异步方法(下)
- [Leetcode] Bulls and Cows
- css实现一行文字居中,多行文字居左
- Expert 诊断优化系列------------------你的CPU高么?
- 浅谈设计模式--组合模式(Composite Pattern)
- Ajax请求SpringMVC
- 【转】VS2012编译出来的程序,在XP上运行,出现“.exe 不是有效的 win32 应用程序” “not a valid win32 application”
- String类 and StringBuffer类
- Jquery 工具类函数
- PHP中Global和Local范围以及Static变量
- PHP 安装 Xdebug 扩展(一)
- ansible 批量安装zabbix agentd客户端
- Collections -- OrderedDict类
- (Solved) Nexiq USB link&#160;with CAT ET: connection error 149
- ElementTriArgyris
- C语言与数据库操作入门
- E 定向 牛客练习赛25
- 管理KVM虚拟机(二)
- IO知识点整理(四种基类的使用)
- 16 go操作Mysql