1 import java.util.LinkedList; 2 3 public class Test5 { 4 5 public static void main(String[] args) { 6 7 Duilie dl = new Duilie(); 8 9 dl.myAdd("abc1"); 10 dl.myAdd("abc2"); 11 dl.myAdd("abc3"); 12 dl.myAdd("abc4"); 13 14 while (!dl.isNull()) 15 { 16 System.out.println(dl.myGet()); 17 } 18 19 } 20 21 } 22 23 class Duilie{ 24 private LinkedList link; 25 26 public Duilie(){ 27 link = new LinkedList(); 28 } 29 30 public void myAdd(Object obj) 31 { 32 link.addLast(obj); 33 } 34 35 public Object myGet() 36 { 37 // return link.removeFirst();//模擬隊列 38 return link.removeLast(); //模擬堆棧 39 } 40 41 public boolean isNull() 42 { 43 return link.isEmpty(); 44 } 45 }