面试题13 - 用两个栈实现队列 【栈】
2013-01-29 13:52
155 查看
#include <iostream> #include <string> #include <stack> #include <queue> #include <algorithm> #define BUG cout << "here\n"; using namespace std; const int N = 105; struct Node { int value; Node* lchild; Node* rchild; }; 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& element) { stack1.push(element); } template<typename T> T CQueue<T>::deleteHead() { if(!stack2.empty()) { T tmp = stack2.top(); stack2.pop(); return tmp; } else { if(stack1.empty()) { cout << "异常" << endl; } while(!stack1.empty()) { // 我感觉这么写更快呢! T tmp = stack1.top(); stack1.pop(); stack2.push(tmp); } T tmp = stack2.top(); stack2.pop(); return tmp; } } int main() { return 0; }
相关文章推荐
- 面试题13 - 用两个栈实现队列 【栈】
- 【面试题7】用两个栈实现队列
- 面试题7:用两个栈实现队列
- C++笔试题(剑指offer 面试题7 两个栈实现队列)
- 剑指offer--面试题7: 用两个栈实现队列
- 面试题之两个队列实现一个栈
- 剑指offer - 面试题7:用两个栈实现队列
- 【练习笔记】剑指offer-面试题7 :用两个栈实现队列
- 面试题7:用两个栈实现队列
- 剑指offer-->面试题7 用两个栈实现队列
- 面试题7:用两个栈实现队列
- 经典面试题:两个栈实现队列+两个队列实现栈----java
- 面试题7 用两个栈实现队列
- 【剑指Offer】面试题7:用两个栈实现队列
- 剑指Offer面试题7用两个栈实现队列(附带用两个队列实现栈)
- 剑指offer面试题 用两个栈实现队列
- 剑指offer面试题7:用两个栈实现队列&用两个队列实现栈
- 面试题7-用两个栈实现队列
- [剑指offer][面试题07]用两个栈实现队列
- 剑指offer之面试题7用两个栈实现一个队列