class MyQueue {
Deque<Integer> inStack;
Deque<Integer> outStack;
public MyQueue() {
inStack = new ArrayDeque<Integer>();
outStack = new ArrayDeque<Integer>();
}
public void push(int x) {
inStack.push(x);
}
public int pop() {
if (outStack.isEmpty()) {
in2out();
}
return outStack.pop();
}
public int peek() {
if (outStack.isEmpty()) {
in2out();
}
return outStack.peek();
}
public boolean empty() {
return inStack.isEmpty() && outStack.isEmpty();
}
private void in2out() {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
}
/**
* 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();
*/
type MyQueue struct {
q []int
size int
}
/** Initialize your data structure here. */
func Constructor() MyQueue {
return MyQueue{
[]int{},
0,
}
}
/** Push element x to the back of queue. */
func (this *MyQueue) Push(x int) {
this.q = append(this.q, x)
this.size++
}
/** Removes the element from in front of queue and returns that element. */
func (this *MyQueue) Pop() int {
if this.size == 0 {
return -1
}
temp := this.q[0]
this.q = this.q[1:this.size]
this.size--
return temp
}
/** Get the front element. */
func (this *MyQueue) Peek() int {
if this.size == 0 {
return -1
}
return this.q[0]
}
/** Returns whether the queue is empty. */
func (this *MyQueue) Empty() bool {
return this.size == 0
}
/**
* Your MyQueue object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
* param_3 := obj.Peek();
* param_4 := obj.Empty();
*/
class MyQueue:
def __init__(self):
"""
Initialize your data structure here.
"""
self.stack = []
def push(self, x: int) -> None:
"""
Push element x to the back of queue.
"""
self.stack.append(x)
def pop(self) -> int:
"""
Removes the element from in front of queue and returns that element.
"""
temp = self.peek()
self.stack.remove(temp)
return temp
def peek(self) -> int:
"""
Get the front element.
"""
return self.stack[0]
def empty(self) -> bool:
"""
Returns whether the queue is empty.
"""
return len(self.stack) == 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()