前言:岁月无情,善待眼前人。
原文出处:http://blog.csdn.net/u014158743/article/details/52629425
//使用LinkedList实现队列
import java.util.*;
class MyQueue
{
private LinkedList link;
public MyQueue()
{
link = new LinkedList();
}
//入队
public void inQueue(Object obj)
{
link.addFirst(obj);
}
//出队
public Object deQueue()
{
return link.removeLast();
}
//判断队列是否为空
public boolean isNull()
{
return link.isEmpty();
}
}
class Demo
{
public static void main(String[] args)
{
MyQueue myQueue = new MyQueue();
myQueue.inQueue("java01");
myQueue.inQueue("java02");
myQueue.inQueue("java03");
while(!myQueue.isNull())
{
System.out.println(myQueue.deQueue());
}
}
}
参考以下方法轻松实现堆栈
从jdk1.6开始出现的新方法
offerFirst()
offerLast()peekFirst()//获取的对象不存在,返回null,不会异常
peekLast()pollFirst()///删除的对象不存在,不会异常,返回null
pollLast()