剑指offer面试题7:用两个栈实现队列
2016-07-10 14:57
337 查看
#include "iostream" #include <string> #include "stack" using namespace std; template <typename T> class cQueue{ public: void appendTail(const T ¬es); T deleteHead(); private: stack<T> stack1; stack<T> stack2; }; template <typename T> void cQueue<T>::appendTail(const T ¬es) { stack1.push(notes); } template <typename T> T cQueue<T>::deleteHead() { if (stack2.size()<=0) { while (stack1.size()>0) { T &data = stack1.top(); stack1.pop(); stack2.push(data); } } if (stack2.size() == 0) throw new exception("queue is empty"); T head = stack2.top(); stack2.pop(); return head; } int main() { cQueue<int> queue; queue.appendTail(1); queue.appendTail(2); queue.appendTail(3); queue.deleteHead(); queue.appendTail(6); queue.deleteHead(); system("pause"); return 0; }
相关文章推荐
- Android面试准备之集合
- 面试准备
- 文件编码方式的分析与理解
- 《剑指Offer》面试题55:字符流中第一个不重复的字符
- 华为机试题【12】-关灯泡
- 华为机试题【12】-关灯泡
- 面试题一帖汇总
- OSChina 周日乱弹 ——致程序员的老婆们
- 最厉害的面试技巧都有哪些?
- 苦逼程序员:半数加班(转)
- 2016腾讯IEG实习面试
- 成为Java顶尖程序员 ,看这11本书就够了
- 剑指offer面试题6:重建二叉树
- 面试题笔记
- 程序员旅游之吐糟途牛——第一天
- 华为机试题【11】-报数删除人
- 剑指offer面试题12 使用BigInteger来实现 打印1到最大的n位数
- 并查集(求朋友圈的个数)
- 《剑指Offer》面试题57:删除链表中重复的节点
- 第140课: Spark面试经典系列之数据倾斜解决之并行度的深度使用