用两个栈实现对列
1.队列:先进先出(FIFO)
2.栈:后进先出(LIFO)
2.栈:后进先出(LIFO)
package test.my.chap0302;
import java.util.Stack;
public class QueueWithTwoStack<E> {
private Stack<E> stack1 = new Stack<E>();
private Stack<E> stack2 = new Stack<E>();
public void appendTail(E e){
stack1.push(e);
}
public EdeleteEle() throws Exception{
if(stack2.size()<=0){
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
}
if(stack2.size()==0){
throw new Exception("Queue is empty!");
}
return stack2.pop();
}
}
浙公网安备 33010602011771号