Java链表

基本链表

package pnunu.node;

class node{
    private String data;
    private node next;

    public String getData(){
        return data;
    }
    
    public node getNext() {
        return next;
    }

    public void setNext(node next) {
        this.next = next;
    }

    public node(String data) {
        this.data = data;
    }
    
    public void nodePrint(){
        System.out.println(this.data);
        if (this.next != null) {
            this.getNext().nodePrint();
        }
    }
    public void addNode(node newnode){
        if (this.next == null){
            this.next = newnode;
        }
        else {
            this.next.addNode(newnode);
        }
    }
}

class Link{
    private node root;
    public void add(String data){
        if (data == null){
            return;
        }
        node newnode = new node(data);
        if (this.root == null){
            this.root = newnode;
        }
        else{
            this.root.addNode(newnode);
        }
    }
    
    /**
     * 打印数据
     */
    public void print(){
        if (this.root != null){
            this.root.nodePrint();
        }
    }
}

public class nodeMode {

    public static void main(String[] args) {
        Link link = new Link();
        link.add("你好");
        link.add("北京");
        link.add("你好");
        link.add("鹤壁");
        link.print();
    }
}

 

posted @ 2016-08-29 15:16  小小暮雨  阅读(147)  评论(0编辑  收藏  举报