Leetcode 232. Implement Queue using Stacks
2016-05-29 11:22
417 查看
Implement the following operations of a queue using stacks.
push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
push(x) – Push element x to the back of queue.
pop() – Removes the element from in front of queue.
peek() – Get the front element.
empty() – Return whether the queue is empty.
Notes:
You must use only standard operations of a stack – which means only push to top, peek/pop from top, size, and is empty operations are valid.
Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
class Queue { stack<int> input, output; public: void push(int x) { input.push(x); } void pop(void) { peek(); output.pop(); } int peek(void) { if (output.empty()) while (input.size()) output.push(input.top()), input.pop(); return output.top(); } bool empty(void) { return input.empty() && output.empty(); } };
相关文章推荐
- C#队列Queue用法实例分析
- 用PHP写的基于Memcache的Queue实现代码
- C#队列Queue多线程用法实例
- Mysql Error Code : 1436 Thread stack overrun
- linux中编写自己的并发队列类(Queue 并发阻塞队列)
- 基于java中stack与heap的区别,java中的垃圾回收机制的相关介绍
- vector,map,list,queue的区别详细解析
- Laravel 4.2 中队列服务(queue)使用感受
- jQuery中队列queue()函数的实例教程
- Python Queue模块详解
- Python多进程通信Queue、Pipe、Value、Array实例
- python使用Queue在多个子进程间交换数据的方法
- Python算法之栈(stack)的实现
- HAZELCAST 客户端命令 可用于简单调试
- Stack数据结构的特点后进先出的应用:大数据运算
- C 堆栈,运行时类型挷定
- jstack和线程dump分析
- EJB3.0 JBoss的JMS实例
- c语言stack实现--数据域与指针域分开
- 深入Java虚拟机