class CQueue {
Stack<Integer> stack;
Stack<Integer> stack1;
public CQueue() {
stack = new Stack<>();
stack1 = new Stack<>();
}
public void appendTail(int value) {
stack.add(value);
}
public int deleteHead() {
if (!stack1.empty()) return stack1.pop();
if (stack.empty()) return -1;
while (!stack.empty()) {
stack1.add(stack.pop());
}
return stack1.pop();
}
}
用两个栈实现队列
最新推荐文章于 2024-02-21 17:10:17 发布