Implement Queue using Stacks
来源:互联网 发布:mysql 命令行导入 编辑:程序博客网 时间:2024/06/05 04:10
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.
- You must use only standard operations of a stack -- which means only
push to top
,peek/pop from top
,size
, andis 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)ja
java
class MyQueue { /** Initialize your data structure here. */ Stack<Integer> stack1; Stack<Integer> stack2; public MyQueue() { stack1 = new Stack<Integer>(); stack2 = new Stack<Integer>(); } private void transfer(Stack<Integer> stack1, Stack<Integer> stack2) { while(!stack1.empty()) { stack2.push(stack1.pop()); } } /** Push element x to the back of queue. */ public void push(int x) { stack1.push(x); } /** Removes the element from in front of queue and returns that element. */ public int pop() { if (stack2.empty()) { transfer(stack1, stack2); } return stack2.pop(); } /** Get the front element. */ public int peek() { if (stack2.empty()) { transfer(stack1, stack2); } return stack2.peek(); } /** Returns whether the queue is empty. */ public boolean empty() { if (stack2.empty()) { transfer(stack1, stack2); } return stack2.empty(); }}/** * Your MyQueue object will be instantiated and called as such: * MyQueue obj = new MyQueue(); * obj.push(x); * int param_2 = obj.pop(); * int param_3 = obj.peek(); * boolean param_4 = obj.empty(); */python
class MyQueue(object): def __init__(self): """ Initialize your data structure here. """ self.stack1 = [] self.stack2 = [] def transfer(self): while len(self.stack1) != 0: self.stack2.append(self.stack1.pop()) def push(self, x): """ Push element x to the back of queue. :type x: int :rtype: void """ self.stack1.append(x) def pop(self): """ Removes the element from in front of queue and returns that element. :rtype: int """ if len(self.stack2) == 0: self.transfer() return self.stack2.pop() def peek(self): """ Get the front element. :rtype: int """ if len(self.stack2) == 0: self.transfer() return self.stack2[-1] def empty(self): """ Returns whether the queue is empty. :rtype: bool """ if len(self.stack2) == 0: self.transfer() return len(self.stack2) == 0# Your MyQueue object will be instantiated and called as such:# obj = MyQueue()# obj.push(x)# param_2 = obj.pop()# param_3 = obj.peek()# param_4 = obj.empty()
阅读全文
1 0
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- keil调试时数组数值显示truncated
- 实验二 命令行菜单小程序V1.0
- <DP版>codevs 3304 水果姐逛水果街Ⅰ
- SQL中的事务
- Error using == vertcat CAT arguments dimensions are not consistent.
- Implement Queue using Stacks
- spring boot启动器
- LCIS 最长上升公共子序列
- 使用AVPlayer遇到的那些坑
- 归并排序python
- Github进行fork后如何与原仓库同步
- Git--远程仓库的使用和多仓库管理
- 项目中的小Tip
- 单点登录原理与简单实现