/*
* @Time : 12/24/2018 21:38
* @Author : Zhang, Chen (chansonzhang)
* @Email : ZhangChen.Shaanxi@gmail.com
* @FileName: DoubleStackQueue.java
*/
public class DoubleStackQueue<T> {
private Stack<T> stackIn;
private Stack<T> stackOut;
public DoubleStackQueue(){
stackIn=new Stack<>();
stackOut=new Stack<>();
}
public T push(T x){
stackIn.push(x);
return x;
}
public T pop(){
shiftStack();
return stackOut.pop();
}
public boolean isEmpty(){
return 0==stackIn.size() && 0==stackOut.size();
}
private void shiftStack(){
if(stackOut.empty()) {
while (!stackIn.isEmpty()) {
stackOut.push(stackIn.pop());
}
}
}
public static void main(String[] args){
DoubleStackQueue<Integer> myQueue=new DoubleStackQueue<>();
for(int i=0;i<10;i++){
myQueue.push(i);
}
while (!myQueue.isEmpty()){
System.out.print(myQueue.pop()+" ");
}
System.out.println();
myQueue.push(0);
myQueue.push(1);
myQueue.push(2);
System.out.print(myQueue.pop()+" ");
myQueue.push(3);
myQueue.push(4);
myQueue.push(5);
while (!myQueue.isEmpty()){
System.out.print(myQueue.pop()+" ");
}
}
}
使用两个栈实现队列
最新推荐文章于 2023-12-06 15:27:43 发布