用两个栈实现对列

1.队列:先进先出(FIFO)
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();
	}
	
	}

posted on 2015-03-23 10:10  Msea  阅读(151)  评论(0编辑  收藏  举报

导航