题目
代码
思路
双栈实现,一个只负责入,另一个只负责出。
class CQueue {
Stack<Integer> stackIn = null;
Stack<Integer> stackOut =null;
public CQueue() {
stackIn = new Stack<>();
stackOut = new Stack<>();
}
public void appendTail(int value) {
stackIn.push(value);
}
public int deleteHead() {
if (stackOut.isEmpty()) {
while (!stackIn.isEmpty()) {
int popValue = stackIn.pop();
stackOut.push(popValue);
}
}
if (stackOut.isEmpty()) {
return -1;
}else {
return stackOut.pop();
}
}
}