package 集合框架2.linkedlist; import java.util.Iterator; import java.util.LinkedList; public class LinkedListDemo1 { public static void main(String[] args) { LinkedList<String> link = new LinkedList<String>(); link.addFirst("abc1"); link.addLast("abc2"); link.add(1, "abc"); Iterator<String> it = link.iterator(); while(it.hasNext()){ System.out.println(it.next()); } // while(!(link.isEmpty())){ // System.out.println(link.removeFirst()); // } // System.out.println(link.getFirst()); // for (Iterator it = link.iterator(); it.hasNext();) { // System.out.println(it.next()); // } for(int x=0;x<link.size();x++){ System.out.println(link.get(x)); } } }
package 集合框架2.linkedlist; import java.util.LinkedList; public class linkedListDemo2 { public static void main(String[] args) { /**使用LinkedList写出一个堆栈或者队列数据结构 * 注意linkedList集合addLast removeLast first的使用*/ DuiZhan<String> d1 = new DuiZhan<String>(); d1.myAdd("abc1"); d1.myAdd("abc2"); d1.myAdd("abc3"); d1.myAdd("abc4"); while(!(d1.Null())){ System.out.println(d1.myGet()); } // DuiLie<String> d2 = new DuiLie<String>(); // d2.myAdd("abc1"); // d2.myAdd("abc2"); // d2.myAdd("abc3"); // d2.myAdd("abc4"); // for(int x=0;x<d2.mySize();x++){ // System.out.println(d2.myGet(x)); // } } } class DuiZhan<T>{ LinkedList<T> link; public DuiZhan(){ link = new LinkedList<T>(); } public void myAdd(T t){ link.add(t); } public Object myGet(){ return link.removeLast(); } public boolean Null(){ return link.isEmpty(); } } class DuiLie<E>{ LinkedList<E> link; public DuiLie(){ link = new LinkedList<E>(); } public void myAdd(E e){ link.add(e); } public Object myGet(int index){ return link.get(index); } public int mySize(){ return link.size(); } }