LinkedStack具體實現並移除參數化,證明內部類可以訪問其外部類的參數類型

public class LinkedStack1<T> {
/*定義鏈棧中的結點 */
 private  class Node {
		 Node next;
		 T item;
		public Node(){
			item = null; next = null;
		}
		public Node(T item,Node next) {
			this.item = item;this.next = next;
		}
		public  boolean end() {
			return item ==null&&next==null;
		}
	}
	// 末端哨兵,用來判斷棧何時爲空。在構建LinkedStack時創建

        private Node top = new Node();
		
	public void push(T item) {
		top = new Node(item,top);
	}
	public T pop() {
		T result = top.item;
		if(!top.end())
			top = top.next;
		return result;
	}
	public static void main(String[] args) {
		LinkedStack1<String> lss = new LinkedStack1<String>();
		for(String s:"rwe dsa das dsa czx".split(" "))
			lss.push(s);
		String s;
		while((s = lss.pop()) != null)
		System.out.println(s);
	}
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章