package test;
import java.util.Stack;
public class StackQueneTest {
private Stack<Integer> stackA = new Stack<Integer>();
private Stack<Integer> stackB = new Stack<Integer>();
/**
* 入队操作
*
* @param element
* 入队的元素
*/
public void enQueue(int element) {
stackA.push(element);
}
/**
* 出队操作
*/
public Integer deQueue() {
if (stackB.isEmpty()) {
if (stackA.isEmpty()) {
return null;
}
transfer();
}
return stackB.pop();
}
/**
* 栈A元素转移到栈B
*/
private void transfer() {
while (!stackA.isEmpty()) {
stackB.push(stackA.pop());
}
}
public static void main(String[] args) throws Exception {
StackQueneTest stackQueue = new StackQueneTest();
stackQueue.enQueue(4);
stackQueue.enQueue(2);
stackQueue.enQueue(7);
System.out.println(stackQueue.deQueue());
System.out.println(stackQueue.deQueue());
stackQueue.enQueue(9);
System.out.println(stackQueue.deQueue());
System.out.println(stackQueue.deQueue());
}
}