標籤:java os new ad ef return public 儲存
<pre name="code" class="java">import java.util.LinkedList;/**棧的定義*/public class Stack<T> {/**定義棧的儲存結構,LinkedList*/private LinkedList<T> storage = new LinkedList<T>();/**壓棧操作*/public void push(T t){storage.addFirst(t); }/**擷取棧頂元素*/public T getTop() {return storage.getFirst(); }/**出棧操作*/public T pop() {return storage.removeFirst(); }/**判斷棧是否為空白*/public boolean isEmpty() {return storage.isEmpty(); }/**列印棧中的元素*/public String toString() {return storage.toString(); } }
import java.util.LinkedList;/**隊列的定義*/public class Queue<T> {/**定義隊列的儲存結構,LinkedList*/private LinkedList<T> storage = new LinkedList<T>();/**進隊操作*/public void push(T t) {storage.addLast(t);}/**擷取隊首元素*/public T getHead() {return storage.getFirst();}/**出隊操作*/public T pop(){return storage.removeFirst();}/**判斷隊列是否為空白*/public boolean isEmpty() {return storage.isEmpty();}/**列印佇列元素*/public String toString(){return storage.toString();}}