栈实现队列,链表实现栈
2016-05-09 13:58
148 查看
栈实现队列:
链表实现栈:
#include<iostream> #include<stack> template<class T>class Queue { std::stack<T> st1, st2; public: void enqueue(T head) { st1.push(head); } T pop() { while (!st1.empty()) { st2.push(st1.top()); st1.pop(); } T tail = st2.top(); st2.pop(); while (!st2.empty()) { st1.push(st2.top()); st2.pop(); } return tail; } }; int main() { Queue<int> qint; for (int i = 1; i < 10; i++) { qint.enqueue(i); } std::cout << qint.pop() << std::endl; system("pause"); return 0; }
链表实现栈:
#include<iostream> #include<list> template<class T>class Stake { std::list<T> tempList; public: void push(T e) { tempList.push_front(e); } T pop() { T tempEl = tempList.front(); tempList.pop_front(); return tempEl; } int length() { int num(0); std::list<T> lList; while (!tempList.empty()) { num++; lList.push_front(tempList.front()); tempList.pop_front(); } while (!lList.empty()) { tempList.push_front(lList.front()); lList.pop_front(); } return num; } }; int main() { Stake<int> testSt; for (int i = 0; i < 10; i++) { testSt.push(i); } //std::cout << testSt.length() << std::endl; for (size_t i = 0; i < testSt.length();) { std::cout << testSt.pop() << std::endl; } system("pause"); return 0; }
相关文章推荐
- PostgreSQL修改数据库表的列属性(ALTER语句)
- Android开发中将状态栏改变成透明颜色
- Android通用流行框架大全
- 微信支付SDK-两行代码解决支付
- 2.1.第五个实验--数码管测试段码
- 用户手势检测-GestureDetector使用详解
- PHP Excel 下载数据,并分页下载
- 判断ListView到达顶端的方法
- 10.Binder进阶:系统服务中的Binder
- ios个人开发者账号 身份证上传不上去
- Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
- 1.5.第四个实验--跑马灯
- 电阻屏的福音
- 技术解析:DAS、SAN和NAS三种存储方式
- Block作为property属性实现页面之间传值(代替Delegate代理与协议结合的方法)
- Django系列四:Django 模型
- Java HashMap工作原理及实现(一)
- 大数据分析的基本办法
- MFC Cbutton、Cstatic、Edit/控件修改字体及颜色
- Spring中应用反射机制浅析