首页 > Linux操作系统 > Linux操作系统 > java 链表上(七)
class Node{
private String name ; // 保存节点的名字
private Node next ; // 保存下一个节点
public Node(String name){
this.name = name ;
}
public void setNext(Node next){
this.next = next ;
}
public Node getNext(){
return this.next ;
}
public String getName(){
return this.name ;
}
};
public class LinkDemo01{
public static void main(String args[]){
Node root = new Node("根节点") ;
Node n1 = new Node("第一节车厢");
Node n2 = new Node("第二节车厢");
Node n3 = new Node("第三节车厢");
root.setNext(n1) ;
n1.setNext(n2) ;
n2.setNext(n3) ;
print(root) ;
}
public static void print(Node node){
if(node!=null){
System.out.print(node.getName() + " --> ") ;
}
if(node.getNext()!=null){
print(node.getNext()) ;
}
}
};
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/15127599/viewspace-666362/ ,如需转载,请注明出处,否则将追究法律责任。