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());
}
}
}
上一篇: PHP的Json中文处理解决方案
下一篇: Java 多线程优先级实例详解