节点类:Node
链表操作类:Link类,专门负责处理节点关系
class Node{ //Node类负责创建节点的属性、方法private String data;private Node next;public Node(){}public Node(String data){this.data=data;}public String getData(){return this.data;}public void setNext(Node next){this.next=next;}public Node getNext(){return this.next;}//增加节点public void addNode(Node newNode){if(this.next == null){this.next=newNode;}elsethis.next.addNode(newNode);}//输出节点public void printNode(){System.out.println(this.data);if(this.next != null){this.next.printNode();}}
}class Link{ //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.printNode();}}
}public class nodeLink {public static void main(String args[]){Link all=new Link();all.add("hello");all.add("world");all.add("MLDN");all.print();}}