用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型
public class Test05 {
Stack<Integer> stackPush=new Stack<>();
Stack<Integer> stackPop=new Stack<>();
public void add(int node){
stackPush.push(node);
}
public int pull(){
if (stackPush.isEmpty()&&stackPop.isEmpty()){
throw new RuntimeException("Queue is empty");
}else if (stackPop.isEmpty()){
while (!stackPush.isEmpty()){
stackPop.push(stackPush.pop());
}
}
return stackPop.pop();
}
public static void main(String[] args) {
Test05 t5=new Test05();
t5.add(1);
t5.add(2);
t5.add(3);
t5.add(4);
System.out.println(t5.pull());
System.out.println(t5.pull());
t5.add(5);
System.out.println(t5.pull());
System.out.println(t5.pull());
System.out.println(t5.pull());
}
}