package day16.linklist;
import java.util.LinkedList;
/*
* 用LinkedList模拟栈结构 写3遍
*/
public class LinkedListDemo {
private LinkedList link;
public LinkedListDemo(){
link = new LinkedList();
}
//添加元素
public void add(Object o){
link.addFirst(o);
}
//获取元素
public Object get(int i){
return link.get(i);
}
//获取长度
public int size(){
return link.size();
}
}
package day16.linklist;
public class StackTest {
public static void main(String[] args) {
LinkedListDemo link = new LinkedListDemo();
link.add("aaa");
link.add("bbb");
link.add("ccc");
//普通for循环遍历
for (int i = 0; i < link.size(); i++) {
System.out.println(link.get(i));
}
}
}
|
|