简单链表实现增删改查(内部类+递归)
链表:
-
一种常见的的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到是下一个节点的指针(Pointer)
-
链表适合插入、删除、不宜过长、否则会导致遍历性能下降
功能实现:
- 添加一个节点
- 查询一个节点
- 删除一个节点
- 修改一个节点
- 打印所有节点
代码实现:递归+内部类
public class Test1 {
public static void main(String[] args) {
NodeManager manager = new NodeManager();
System.out.println("------add-------");
manager.add("1");
manager.add("2");
manager.add("3");
manager.add("4");
manager.add("5");
manager.print();
System.out.println("----------del----------");
manager.del("3");
manager.print();
System.out.println("----------update---------");
manager.update("5", "6");
manager.print();
System.out.println("----------find------------");
System.out.println(manager.find("2"));
System.out.println("----------insert---------");
System.out.println(manager.insert(2, "qijing"));
manager.print();
}
}
class NodeManager {
private Node node; // 根节点
private int currentIndex = 0;// 节点的序号
// 添加的方法
public void add(String date) {
if (node == null) {
node = new Node(date);
} else {
node.add(date);
}
}
// 删除的方法
public void del(String date) {
if (node != null) {
if (node.getDate() == date) {
node = node.next;
} else {
node.del(date);
}
}
}
// 打印所有节点
public void print() {
if (node != null) {
System.out.print(node.getDate() + "->");
node.print();
System.out.println();
}
}
//插入一个节点后面
public boolean insert(int index, String date) {
currentIndex = 0;
if (node == null) {
return false;
}
if (index == currentIndex) {
Node node1 = new Node(date);
node1.next = node.next;
node.next = node1;
return true;
} else {
return node.insert(index, date);
}
}
// 查询是否存在节点
public boolean find(String date) {
if (node == null) {
return false;
}
if (node.getDate() == date) {
return true;
} else {
return node.find(date);
}
}
//修改一个节点
public void update(String oldNode, String newNode) {
if (node != null) {
if (node.getDate() == oldNode) {
node.setDate(newNode);
} else {
node.update(oldNode, newNode);
}
}
}
private class Node {
private String date;
private Node next;
public Node(String date) {
this.date = date;
}
public void setDate(String date) {
this.date = date;
}
public String getDate() {
return this.date;
}
// 添加的方法
public void add(String date) {
if (this.next == null) {
this.next = new Node(date);
} else {
this.next.add(date);
}
}
// 查询是否存在节点
public boolean find(String date) {
if (this.next == null) {
return false;
}
if (this.next.date == date) {
return true;
} else {
return this.next.find(date);
}
}
// 删除的方法
public void del(String date) {
if (this.next != null) {
if (this.next.getDate() == date) {
this.next = this.next.next;
} else {
this.next.del(date);
}
}
}
// 打印所有节点
public void print() {
if (this.next != null) {
System.out.print(this.next.date + "->");
this.next.print();
}
}
//插入一个节点后面
public boolean insert(int index, String date) {
if (this.next == null) {
return false;
}
currentIndex++;
if (currentIndex == index) {
Node node1 = new Node(date);
node1.next = this.next.next;
this.next.next = node1;
return true;
}
return this.next.insert(index, date);
}
//修改一个节点
public void update(String oldNode, String newNode) {
if (this.next != null) {
if (this.next.date == oldNode) {
this.next.date = newNode;
} else {
this.next.update(oldNode, newNode);
}
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!