简单单向链表
-
class Node{
-
private String data;
-
private Node next=null;
-
public Node(String data){
-
this.setDate(data);
-
}
-
public void setDate(String data){
-
this.data = data;
-
}
-
public void setNext(Node next){
-
this.next = next;
-
}
-
public String getDate(){
-
return this.data;
-
}
-
public Node getNext(){
-
return this.next;
-
}
-
}
-
public class LinkDemo01
-
{
-
public static void main(String args[]){
-
Node n1 = new Node("节点-A");
-
Node n2 = new Node("节点-B");
-
Node n3 = new Node("节点-C");
-
Node n4 = new Node("节点-D");
-
n1.setNext(n2);
-
n2.setNext(n3);
-
n3.setNext(n4);
-
printNode(n1);
-
}
-
public static void printNode(Node node){
-
System.out.println(node.getDate());
-
if(node.getNext()!=null){
-
printNode(node.getNext());
-
}
-
}
-
}
单向链表整合内部类
总结:
1.类的职能不同,LinkDemo01是基础链表类,而LinkDemo02的内部类为基础链表类,外部类为链表操作类。
2.基础链表类中存在一个this.next指向下一个链表对象。
java 单向链表
原文:http://blog.csdn.net/sqc3375177/article/details/45243643