You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
685 B
685 B
栈相关
09. 用两个栈实现队列
class CQueue {
Stack<Integer> putStack, takeStack;
public CQueue() {
putStack = new Stack<>();
takeStack = new Stack<>();
}
public void appendTail(int value) {
putStack.push(value);
}
public int deleteHead() {
if (takeStack.isEmpty()) {
while (!putStack.isEmpty()) {
takeStack.push(putStack.pop());
}
}
if (takeStack.isEmpty()) {
return -1;
} else {
return takeStack.pop();
}
}
}