一.定义一个节点
创建一个节点类,类中定义两个属性,一个属性用来存放数据,一个属性用来存放下一个节点
class Node {
String data = null; //链表存储的数据
Node next = null; //下一个节点
public Node (String data) {
this.data = data;
}
public void setNext(Node next) {
this.next = next;
}
public Node getNext() {
return next;
}
public String getData() {
return data;
}
}
二.遍历链表
1.使用循环进行遍历
public static void getDataByLoop(Node node){
while (node != null) {
System.out.println(node.getData());
node = node.getNext();
}
}
2.使用递归遍历
public static void getDataByRecursion(Node node) {
if (node == null) {
return;
}
System.out.println(node.getData());
getDataByRecursion(node.getNext());
}
三.创建节点设置节点关系
public static void main(String[] args){
Node root = new Node("头结点");
Node node01 = new Node("子节点01");
Node node02 = new Node("子节点02");
root.setNext(node01); //将节点连接起来
node01.setNext(node02);
System.out.println("使用while循环遍历链表");
getDataByLoop(root);
System.out.println("使用递归遍历链表");
getDataByRecursion(root);
}