一、题目描述
用两个栈实现一个队列,完成队列的Push和Pop操作。
二、解题思路
三、代码实现
Stack<Integer> in = new Stack<Integer>();
Stack<Integer> out = new Stack<>();
public void push(int node){
in.push(node);
}
public int pop() throws Exception{
if(out.isEmpty()){
while(! in.isEmpty()){
out.push(in.pop());
}
}
if(out.isEmpty()){
throw new Exception("Queue is empty");
}
return out.pop();
}