LinkedList实现队列和堆栈的代码
package 集合;
import java.util.LinkedList;
/*
*队列:先进先出
*把romovelast改成romoveFirst就成了堆栈 先进后出
*
* */
public class DuiLie {
private LinkedList link;
public DuiLie(){
link = new LinkedList();
}
//添加到第一个
public void myAdd(Object obj){
link.addFirst(obj);
}
//得到最后一个
public Object myGet(){
return link.removeLast();
}
//判断是否为空
public boolean isNull(){
return link.isEmpty();
}
}
class LinkedListTest{
public static void main(String[] args) {
DuiLie dl = new DuiLie();
dl.myAdd("java1");
dl.myAdd("java2");
dl.myAdd("java3");
dl.myAdd("java4");
while(!dl.isNull()){
System.out.println(dl.myGet());
}
}
}