剑指offer编程题——07 两个栈实现队列
2018-03-16 14:29
513 查看
#include <iostream> #include <exception> #include <stack> using namespace std; template<typename T> class CQueue{ private: stack<T> stack1; stack<T> stack2; public: CQueue(); ~CQueue(); void appendTail(const T& element); T deleteHead(); int size(); }; template<typename T> CQueue<T>::CQueue(){}; template<typename T> CQueue<T>::~CQueue(){}; template<typename T> void CQueue<T>::appendTail(const T& element){ stack1.push(element); } 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 exception("queue is empty."); T head = stack2.top(); stack2.pop(); return head; } template<typename T> int CQueue<T>::size(){ return stack1.size() + stack2.size(); } void main07(){ CQueue<int> cq; int elem[10] = {0}; cout << "enter order: "; for (int i = 5; i > 0; --i){ cq.appendTail(i); cout << i << " "; } cout << endl << "out order: "; for (int i = 0; i < 5; ++i){ elem[i] = cq.deleteHead(); cout << elem[i] << " "; } return; }
相关文章推荐
- 【剑指offer-Java版】07用两个栈实现队列
- 剑指offer编程题Java实现——面试题7用两个栈实现队列
- 剑指Offer-【面试题07:两个栈实现队列】
- [剑指offer][面试题07]用两个栈实现队列
- 剑指offer-面试题07-用两个栈实现队列
- 【newCoder】剑指Offer_编程题 用两个栈实现队列
- 剑指offer—编程题7(用两个栈实现一个队列)
- 剑指offer 编程题(5):两个栈实现队列,两个队列实现栈
- 剑指offer-07(1):两个队列实现栈
- 剑指Offer_面试题07_用两个栈实现队列
- 剑指offer-面试题07-衍生题目:用两个队列实现栈
- 剑指Offer_07 用两个栈实现队列与 用两个队列实现栈
- [剑指offer][面试题07-扩展]用两个队列实现栈
- 剑指offer-07:两个栈实现队列
- 【剑指offer】面试题07:用两个栈实现队列
- 【剑指offer】两个栈实现一个队列
- 剑指offer-两个栈实现队列
- 剑指offer之用两个栈实现队列(java实现)
- 【剑指offer】用两个栈实现队列
- 剑指offer题7_用两个栈实现队列