在添加数据的时候就把数据结构处理好
class MyQueue {
Stack que = new Stack<>();
// Push element x to the back of queue.
public void push(int x) {
Stack<Integer> temp = new Stack<>();
if(que.isEmpty()) {
que.push(x);
} else {
while(!que.isEmpty()) {
temp.push(que.pop());
}
que.push(x);
while(!temp.isEmpty()) {
que.push(temp.pop());
}
}
}
// Removes the element from in front of queue.
public void pop() {
que.pop();
}
// Get the front element.
public int peek() {
return que.peek();
}
// Return whether the queue is empty.
public boolean empty() {
return que.isEmpty();
}
在添加数据的时候就把数据结构处理好 class MyQueue { Stack que = new Stack<>();
}