没啥好说的
public MyQueue() {
Stack<Integer> stack1=new Stack();
Stack<Integer> stack2=new Stack();
}
Stack<Integer> stack1=new Stack();
Stack<Integer> stack2=new Stack();
/** 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() {
int x;
while(stack1.size()!=0){
stack2.push(stack1.pop());
}
if(stack2.size()!=0){
x=stack2.pop();
}else x=-1;
while (stack2.size()!=0){
stack1.push(stack2.pop());
}
return x;
}
/** Get the front element. */
public int peek() {
int x;
while(stack1.size()!=0){
stack2.push(stack1.pop());
}
if(stack2.size()!=0){
x=stack2.peek();
}else x=-1;
while (stack2.size()!=0){
stack1.push(stack2.pop());
}
return x;
}
/** Returns whether the queue is empty. */
public boolean empty() {
if(stack1.isEmpty())return true;
else return false;
}