思路:添加元素就往stack1中压入,想删除就从stack2中弹出,stack2是从stack1pop出来压入的
class CQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
public CQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
public void appendTail(int value) {
stack1.add(value);
}
public int deleteHead() {
if (stack2.isEmpty() && stack1.isEmpty()) return -1;
if (!stack2.isEmpty()) return stack2.pop();
while (!stack1.isEmpty()) {
stack2.add(stack1.pop());
}
return stack2.pop();
}
}