剑指offer系列-用两个栈实现队列
来源:互联网 发布:smi s java 编辑:程序博客网 时间:2024/04/29 09:42
oj地址
#include<iostream>#include<stdio.h>#include<string.h>#include<stack>using namespace std;template <typename T> class CQueue{ public: //CQueue(void); //~CQueue(void); void appendTail(const T& node); T deleteHead(); private: stack<T> stack1; stack<T> stack2;};template <typename T> void CQueue<T>::appendTail(const T& node){ stack1.push(node);}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 "error"; } T head = stack2.top(); stack2.pop(); return head;}int main(){ int n; char ch[10]; int num; CQueue<int> cqueue; scanf("%d",&n); while(n--){ scanf("%s",ch); if(strcmp("PUSH",ch)==0){ scanf("%d",&num); cqueue.appendTail(num); }else{ try{ printf("%d\n",cqueue.deleteHead()); }catch(const char * str){ printf("-1\n"); } } } return 0;}
0 0
- 剑指offer系列-用两个栈实现队列
- 剑指offer系列之五:用两个栈实现队列
- 剑指offer系列-用两个栈实现队列
- 剑指offer系列-用两个队列实现栈
- 剑指offer系列----用两个栈实现一个队列
- 剑指Offer系列---(9)用两个栈实现队列和用两个队列实现栈
- [剑指offer]用两个栈实现队列
- 【剑指offer】用两个栈实现队列
- 剑指offer--用两个栈实现队列
- 剑指Offer-用两个栈实现队列
- 剑指offer:用两个栈实现队列
- 《剑指offer》用两个栈实现队列
- 用两个栈实现队列(剑指offer)
- 剑指 offer:用两个栈实现队列
- 【剑指Offer】用两个栈实现队列
- 剑指offer-用两个栈实现队列
- 《剑指offer》-用两个栈实现队列
- 剑指offer 用两个栈实现队列
- Mysql高负载排查思路
- 修改sublime快捷键与eclipse一样
- Best Time to Buy and Sell Stock -- LeetCode
- 对java中常用的集合几点看法
- 单例模式
- 剑指offer系列-用两个栈实现队列
- 什么是OBS
- Python起个web服务
- 非洲小孩
- 工厂模式
- 大数据架构:flume-ng+Kafka+Storm+HDFS 实时系统组合
- nyoj题目13:Fibonacci数
- Spring原理探索第一步-以excel导入为列探讨SpringIoc原理
- 如何估算网站日承受最大访问PV