剑指offer第7题 两个栈实现队列
来源:互联网 发布:百度大数据 编辑:程序博客网 时间:2024/06/05 21:51
//两个栈实现队列#include <iostream>#include <stack>#include <cstdio>using namespace std;template <typename T> class myQueue{public:myQueue(void){}~myQueue(void){}void appendTail(const T& node);T deleteHead();int size() const;private:stack<T> stack1;stack<T> stack2;};template <typename T> void myQueue<T>::appendTail(const T& node){stack1.push(node);}template <typename T> T myQueue<T>::deleteHead(){if(stack2.empty()){while(!stack1.empty()){T node = stack1.top();stack1.pop();stack2.push(node);}}if(stack2.empty())throw std::exception("queue is empty");T node = stack2.top();stack2.pop();return node;}template <typename T> int myQueue<T>::size() const{return stack1.size()+stack2.size();}int main(){myQueue<int> que;que.appendTail(5);que.appendTail(4);que.appendTail(3);que.appendTail(10);while(que.size()!=0){cout<<que.deleteHead()<<" ";}cout<<endl;return 0;}
- 剑指offer第7题 两个栈实现队列
- 用两个栈实现队列 (剑指Offer 第 5 题)
- [剑指offer]用两个栈实现队列
- 【剑指offer】两个栈实现一个队列
- 【剑指offer】两个队列实现一个栈
- 【剑指offer】用两个栈实现队列
- 剑指offer--用两个栈实现队列
- 剑指offer 两个栈实现队列
- 剑指Offer-用两个栈实现队列
- 剑指offer:用两个栈实现队列
- 《剑指offer》用两个栈实现队列
- 用两个栈实现队列(剑指offer)
- 剑指 offer:用两个栈实现队列
- 【剑指Offer】用两个栈实现队列
- 剑指offer-用两个栈实现队列
- 《剑指offer》-用两个栈实现队列
- 剑指offer 用两个栈实现队列
- 剑指offer 用两个栈实现队列
- JQuery实现拖动删除效果
- PHP中strpos函数的使用说明
- 内核模块的编写、编译、加载、卸载;Makefile的编写
- $(addprefix <prefix>,<names...> )
- 愁容骑士
- 剑指offer第7题 两个栈实现队列
- linux下强制不检测依赖安装VNC
- java实现栈
- VirtualBox 安装虚拟机时出现错误 VT-x features locked or unavailable in MSR.
- activiti5 流程文件中使用java 方法
- 第十一周上机任务项目2
- NodeJS 常用模块推荐
- posix多线程有感--线程高级编程(线程内存模式)
- 给redhat 6.0配置yum源