·链表是一种最为简单的数据结构,它的主要目的是依靠引用关系来实现多个数据的保存,下面代码以String字符串类型示例
class Node {
private String data;
private Node next;
public Node(String data) {
this.data = data;
}
public void setNext(Node next) {
this.next = next;
}
public Node getNext() {
return this.next;
}
public String getData() {
return this.data;
}
}
public class NodeTest {
public static void main(String[] args) throws Exception {
// 1.准备所有数据
Node no1 = new Node("贪玩揽月");
Node no2 = new Node("渣渣辉");
Node no3 = new Node("古天乐");
// 2.链接数据
no1.setNext(no2);
no2.setNext(no3);
// 3.取出数据
print(no1);
}
// 打印方法采取递归调用
public static void print(Node current) {
if (current == null) {// 递归调用退出条件
return;// 结束递归
}
System.out.println(current.getData());
print(current.getNext());
}
}