class MyQueue {
Deque<Integer> stack1;
Deque<Integer> stack2;
public MyQueue() {
stack1 = new LinkedList<>();
stack2 = new LinkedList<>();
}
public void push(int x) {
stack1.push(x);
}
public int pop() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
public int peek() {
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
}
}
class MyStack {
Queue<Integer> queue;
public MyStack() {
this.queue = new LinkedList<>();
}
public void push(int x) {
queue.offer(x);
}
public int pop() {
int size = queue.size();
for (int i = 0; i < size - 1; i++) {
queue.offer(queue.poll());
}
return queue.poll();
}
public int top() {
int size = queue.size();
int res = 0;
for (int i = 0; i < size; i++) {
if (i == size - 1) res = queue.peek();
queue.offer(queue.poll());
}
return res;
}
public boolean empty() {
return queue.isEmpty();
}
}