代码改变世界

Java用LinkedList实现队列

2012-07-27 17:16  javaspring  阅读(399)  评论(0编辑  收藏  举报
import java.util.LinkedList;


public class MyQueue {
	private LinkedList ll=new LinkedList();
	public void put(Object o)
	{
		ll.addLast(o);
	}
	public Object get()
	{
		if(ll.isEmpty())
		{
			System.out.println("队列为空,不能出队列了");
			return null;
		}
		return ll.removeFirst();
	}
	public boolean empty()
	{
		return ll.isEmpty();
	}
	public static void main(String []args)
	{
		MyQueue mq=new MyQueue();
		mq.put("one");
		mq.put("two");
		mq.put("three");
		System.out.println(mq.get());
		System.out.println(mq.get());
		System.out.println(mq.get());
		
		
		System.out.println(mq.empty());
	}

}

输出结果为

one
two
three
true