1.单项链表---打印火车
//定义火车节点
class Node
{
private String data = null ;
Node node = null ;
public Node(String data){
this.data = data ;
}
public void setNode(Node node){
this.node = node ;
}
public Node getNode(){
return this.node ;
}
public String getData(){
return this.data ;
}
}
public class LinkDemo01
{
public static void main(String args[]){
Node root = new Node("火车头") ;
Node n1 = new Node("火车A") ;
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() + "\t") ;
if(node.getNode() != null){
printNode(node.getNode()) ;
}
}
}