源程序:
Node n1 = new Node("车厢-A") ;
class Node{
private String data ;
private Node next ;
public Node(String data){
this.setDate(data) ;
}
public void setDate(String data){
this.data = data ;
}
public String getData(){
return this.data ;
}
public void setNode(Node next){
this.next = next ;
}
public Node getNode(){
return this.next ;
}
}
public class Test{
public static void main(String[] args){
Node root = new Node("火车头") ;
Node n2 = new Node("车厢-B") ;
Node n3 = new Node("车厢-C") ;
root.setNode(n1) ;
n1.setNode(n2) ;
n2.setNode(n3) ;
printNode(root) ;
}
public static void printNode(Node node){
System.out.println(node.getData()) ;
if(node.getNode()!=null){
printNode(node.getNode()) ;
}
}
}
运行结果: