java單鏈表的創建及輸出

class Node{
    private Object data;
    private Node next;
    public Node(Object data) {
        this.data = data;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }
    public Node getNext() {
        return next;
    }
    public void setNext(Node next) {
        this.next = next;
    }
    public void getList(Node node) {
        while(node!=null) {
            System.out.println(node.data);
            node=node.next;
        }
        
    }
}
public class LianBiao {
    public static void main(String[] args) {
        Node first=new Node("火車頭");
        Node a1= new Node("車廂一");
        Node a2=new Node("車廂二");
        Node a3=new Node("車尾");
        first.setNext(a1);
        a1.setNext(a2);
        a2.setNext(a3);
        first.getList(first);
    }    

}
 

發佈了82 篇原創文章 · 獲贊 10 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章