class Node{
private String data;
private Node next;
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 class Link{
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.setNext(n1);
n1.setNext(n2);
n2.setNext(n3);
printNode(root);//从根节点输出
}
public static void printNode(Node node){
System.out.print(node.getData()+"\t");//输出数据
if(node.getNext()!=null){//判断结点是否为空
printNode(node.getNext());//递归,继续向下打印
}
}
}
单向链表
最新推荐文章于 2023-07-26 00:47:03 发布