吴裕雄--天生自然数据结构与算法:java代码实现常用数据结构——链表Linked List

class Node {
    private String data;
    private Node head;

    public Node(String data) {
        this.data = data;
    }

    public Node getNode() {
        return this.head;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getData() {
        return this.data;
    }
    //添加节点
    public void addNode(Node newNode) {
        Node temp = this.head;
        while (this.head != null) {
            temp = this.head.head;
        }
        this.head = newNode;
    }
}

public class Demo2_LinkedList {
    public static void main(String[] args) {
        Node n1 = new Node("s1");
        Node n2 = new Node("s2");
        Node n3 = new Node("s3");
        Node n4 = new Node("s4");
        n1.addNode(n2);
        n2.addNode(n3);
        n3.addNode(n4);
        System.out.println(n1.getData());
        System.out.println(n1.getNode().getData());
        System.out.println(n3.getData());
        System.out.println(n3.getNode().getData());
    }
}

 正在更新中...

猜你喜欢

转载自www.cnblogs.com/tszr/p/12210653.html