简单链表实现增删改查(内部类+递归)

简介: 简单链表实现增删改查(内部类+递归)

链表:

  • 一种常见的的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到是下一个节点的指针(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);
                }
            }
        }
    }
}


目录
相关文章
|
5月前
【每日一题Day287】LC24 两两交换链表中的节点 | 模拟 递归
【每日一题Day287】LC24 两两交换链表中的节点 | 模拟 递归
38 0
|
5月前
|
算法
算法系列--递归(一)--与链表有关(上)
算法系列--递归(一)--与链表有关
61 0
|
2月前
|
存储 Java
java实现双向链表的增删改查
这篇文章展示了如何在Java中实现双向链表的增加、删除、修改和查询操作,并通过代码示例演示了在双向链表中存储和操作学生信息的过程。
|
4月前
|
SQL 算法 数据可视化
LeetCode题目92:反转链表ll 【python 递归与迭代方法全解析】
LeetCode题目92:反转链表ll 【python 递归与迭代方法全解析】
实验:数据结构(结构体在单链表中的增删改查)
实验:数据结构(结构体在单链表中的增删改查)
|
5月前
|
算法
合并有序链&&反转链表(递归版)
合并有序链&&反转链表(递归版)
|
5月前
|
算法
算法系列--递归(一)--与链表有关(下)
算法系列--递归(一)--与链表有关(下)
32 0
|
5月前
|
存储 算法 C语言
【数据结构与算法】【约瑟夫问题】还在用递归?教你用链表秒杀约瑟夫
【数据结构与算法】【约瑟夫问题】还在用递归?教你用链表秒杀约瑟夫
|
5月前
|
算法
每日一题——排序链表(递归 + 迭代)
每日一题——排序链表(递归 + 迭代)
|
5月前
【每日一题Day286】LC21合并两个有序链表 | 链表模拟 递归
【每日一题Day286】LC21合并两个有序链表 | 链表模拟 递归
29 0