欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java实现单链表

程序员文章站 2024-03-01 09:18:40
...
java实现单链表

class Node{
private String name;
private Node next;

public Node(String name){
this.name = name;
}
public void setName(String name){
this.name = name;
}

public String getName(){
return this.name;
}

public void setNext(Node next){
this.next = next;
}
public Node getNext(){
return this.next;

}
}

public class LinkDemo {
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);
printNode(root);
}

public static void printNode(Node node){
if(node!=null){
System.out.print(node.getName()+"-->");
}
if(node.getNext()!=null){
printNode(node.getNext());
}
}

}


相关标签: Java