用栈来实现队列
import java.util.Stack;
class MyQueue {
Stack<Integer> inStack = new Stack<>();
Stack<Integer> outStack = new Stack<>();
public MyQueue() {
}
public void push(int x) {
inStack.push(x);
}
public int pop() {
if (outStack.empty() == true && inStack.empty() == true) {
empty();
}
dump();
return outStack.pop();
}
public int peek() {
if (outStack.empty() == true && inStack.empty() == true) {
empty();
}
dump();
return outStack.peek();
}
public boolean empty() {
if (inStack.empty() == true && outStack.empty() == true) {
return true;
}
return false;
}
public void dump() {
if (outStack.empty() == true && inStack.empty() != true) {
while (inStack.empty() != true) {
outStack.push(inStack.pop());
}
}
}
}