题目
用两个栈实现一个队列,队列的声明如下,请实现它的两个函数appendTail
和deleteHead,分别完成在队列尾部插入整数和在队列头部删除整数的功能。
(若队列中没有元素,deleteHead返回-1)。
代码
class CQueue {
private Stack<Integer> stackPush = new Stack<Integer>();
private Stack<Integer> stackPop = new Stack<Integer>();
public CQueue() {
}
public void appendTail(int value) {
stackPush.push(value);
}
public int deleteHead() {
if(stackPush.isEmpty() && stackPop.isEmpty()){
return -1;
}
if(stackPop.isEmpty()){
while(!stackPush.isEmpty()){
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
}