队列实现栈
2015-11-18 23:01
267 查看
队列实现栈
两个队列实现栈:
code:
一个队列实现栈:
两个队列实现栈:
code:
#include <stdio.h> #include <queue> using namespace std; template<typename T> class Stack { public: bool empty() { return q1.empty() && q2.empty(); } void put(const T &e) { q1.push(e); } T remove() { while (q1.size() > 1) { T t = q1.front(); q1.pop(); q2.push(t); } T tmp = q1.front(); q1.pop(); while (!q2.empty()) { T t = q2.front(); q2.pop(); q1.push(t); } return tmp; } private: queue<T> q1, q2; }; int main() { Stack<int> s; s.put(1); s.put(2); s.put(3); while (!s.empty()) printf("%d ", s.remove()); getchar(); return 0; }
一个队列实现栈:
#include <stdio.h> #include <queue> using namespace std; template<typename T> class Stack { public: bool empty() { return que.empty(); } void push(const T &e) { que.push(e); for (int i = 0; i < que.size() - 1; i++) { que.push(que.front()); que.pop(); } } void pop() { return que.pop(); } T top() { return que.front(); } private: queue<T> que; }; int main() { Stack<int> s; s.push(1); s.push(2); s.push(3); while (!s.empty()) { printf("%d ", s.top()); s.pop(); } getchar(); return 0; }
相关文章推荐
- 第17篇 基于Bootstarp 仿京东多条件筛选插件的开发(展示下)
- CBitmapButton为按钮加载图片注意事项
- 简单工厂模式,抽象工厂模式,反射工厂模式的代码总结
- SPOJ NSUBSTR
- EasyUI+MVC4实现后台管理系统一:登陆和进入后台界面
- 用三或四个个div标签实现工字效果
- 不懂的linux
- android开发笔记:listview
- 第16篇 基于Bootstarp 仿京东多条件筛选插件的开发(展示上)
- 在svn客户端连接时提示错误的处理办法
- Java中提供的工具类
- 冒泡排序算法(C#实现)
- 第15篇 文件流和内存流的学习总结
- [MFC] CDialog::DoModal()函数用法
- 自动化部署工具pssh入门教程
- 深入理解C#:查询集合
- jsp include指令
- Python 学习LINK
- Android 指纹扫描动画实现,仿新版360手机助手欢迎页
- hadoop集群安装好之后的启动操作