支持队列的基本操作add/poll/peek
public class TwoStackQueue{
public Stack<Integer> stackPush;
public Stack<Integer> stackPop;
public TwoStackQueue(){
stackPush=new Stack<Integer>();
stackPop=new Stack<Integer>();
}
public void add(int value){
stackPush.push(value)
}
public void shiftStacks(){
if(stackPop.empty()&&stackPush.empty()){
throw new RuntimeException("Queue is empty!");
}
else if(stackPop.empty()){
while(!stackPush.empty()){
stackPop.push(stackPush.pop());
}
}
}
public int peek(){
shiftStacks();
return stackPop.peek();
}
public int poll(){
shiftStacks();
return stackPop.pop();
}
}
网友评论