java 實現 堆棧

package com.fanxing.lining.last;

 

public class LinkStack <T>{

private class Node<U>{

U item;

Node<U> next;

Node(){

this.item=null;

this.next=null;

}

Node(U item,Node<U> next){

this.item=item;

this.next=next;

}

boolean empty(){

return this.item==null&&this.next==null;

}

}

Node<T> top=new Node<T>();

public void push(T t){

top=new Node<T>(t,top);

}

public T pop(){

T result=top.item;

if(!top.empty())

top=top.next;

return result;

}

public static void main(String[] args) {

LinkStack<String> ls=new LinkStack<String>();

for(int i=0;i<10;i++)

ls.push("str"+i);

String s;

while((s=ls.pop())!=null)

System.out.println(s);

System.out.println(ls.top.empty());

}

}

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