栈是先入后出,队列是先入先出
定义两个栈,stack1,stack2,分别用来作为添加元素的添加栈,删除元素的删除栈
class CQueue {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
public CQueue() {
stack1=new Stack<>();
stack2=new Stack<>();
}
public void appendTail(int value) {
while(!stack2.isEmpty()){
stack1.push(stack2.pop());
}
stack1.push(value);
}
public int deleteHead() {
if(!stack2.isEmpty()){
return stack2.pop();
}
if(stack1.isEmpty()){
return -1;
}
else{
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
return stack2.pop();
}
}
}