3.4 Queue via Stacks
2015-10-27 12:35
387 查看
Using two stacks to implement queue.
class Queue { public: stack<int> stkIn; stack<int> stkOut; // Push element x to the back of queue. void push(int x) { stkIn.push(x); } // Removes the element from in front of queue. void pop(void) { peek(); stkOut.pop(); } // Get the front element. int peek(void) { if(stkOut.empty()){ while(stkIn.size()){ stkOut.push(stkIn.top()); stkIn.pop(); } } return stkOut.top(); } // Return whether the queue is empty. bool empty(void) { return (stkOut.empty() && stkIn.empty()); } };
相关文章推荐
- Mysql Error Code : 1436 Thread stack overrun
- 基于java中stack与heap的区别,java中的垃圾回收机制的相关介绍
- Python算法之栈(stack)的实现
- Stack数据结构的特点后进先出的应用:大数据运算
- C 堆栈,运行时类型挷定
- jstack和线程dump分析
- c语言stack实现--数据域与指针域分开
- 深入Java虚拟机
- 搭建CloudStack
- 新手,正在学Java Collection,瞎写点东西-一个基于链表的stack及其遍历
- 转贴一份挺好的堆和栈区别的资料
- parse in programming
- 百田笔试题
- Binary Tree Preorder Traversal
- [LeetCode] Valid Parenthese
- [Leetcode] Generate Parentheses
- [Leetcode] Postorder Traverse of Binary Tree
- [LeetCode] Valid Parentheses
- Binary Tree Zigzag Level Order Traversal
- Min Stack