模拟实现单链表 —— SingleLinkedList

模拟实现 java 中单链表的实现,方便后续对 java 中的 LInkedList 进行理解。

MySingleList类:

java 复制代码
public class MySingleList {

    /**
     * 定义节点类
     */
    static class ListNode {
        // 节点值
        private int val; 
        // 下一个节点的引用
        private ListNode next; 

        public ListNode(int val) {
            this.val = val;
        }
    }

    // 头节点
    private ListNode head;

    /**
     * 头插法
     */
    public void headInsert(int value) {
        // 申请节点
        ListNode node = new ListNode(value);

        // 头插
        node.next = head;
        head = node;
    }

    /**
     * 尾插法
     */
    public void tailInsert(int value) {
        // 申请节点
        ListNode node = new ListNode(value);

        // head 为空
        if (head == null) {
            // head 直接指向 node
            head = node;
            return;
        }

        ListNode cur = head;
        // 找最后一个节点
        while (cur.next != null) {
            cur = cur.next;
        }

        cur.next = node;
    }

    /**
     * 任意位置插入
     */
    public void randomInsert(int index, int value) {
        // 判断下标是否合理
        judge(index);

        // 判断是否是头插或尾插
        if (isInsert(index, value)) {
            return;
        }

        // 中间位置插入
        ListNode cur = head;
        // 找到插入位置的前一个位置
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }

        // 插入
        ListNode node = new ListNode(value);
        node.next = cur.next;
        cur.next = node;

    }

    /**
     * 判断下标是否合理
     */
    private void judge(int index) {
        if (index < 0 || index > size()) {
            throw new IndexOFBoundException("下标: " + index + "错误!");
        }
    }

    /**
     * 判断插入位置是否是头插或尾插
     */
    private boolean isInsert(int index, int value) {
        if (index == 0) { // 头插
            headInsert(value);
            return true;
        } else if (index == size()) { // 尾插
            tailInsert(value);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查找 value 是否在链表中存在
     */
    public boolean contains(int value) {
        ListNode cur = head;

        while (cur != null) {
            if (cur.val == value) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    /**
     * 删除链表中第一次出现 value 的节点
     */
    public void remove(int value) {
        // head 为空或要删除的节点是 head
        if (isNullOrHead(value)) {
            return;
        }

        // 获取要删除节点的前一个节点
        ListNode preNode = findFirstDeletePreNode(value);
        // 判断 preNode 为空
        if (preNode == null) {
            System.out.println("没有这个元素对应的节点");
            return;
        }

        // 删除节点
        ListNode delNode = preNode.next;
        preNode.next = delNode.next;
    }

    /**
     * 要删除的节点为空或是第一个节点
     */
    private boolean isNullOrHead(int value) {
        if (head == null) { // head 为空
            return true;
        } else if (head.val == value) { // head 的 val 和 value 相同
            // head 后移
            head = head.next;
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查找要删除 value 节点
     */
    private ListNode findFirstDeletePreNode(int value) {
        ListNode cur = head;
        while (cur.next != null) {
            // 找到了要删除节点的前驱
            if (cur.next.val == value) {
                return cur;
            }
            cur = cur.next;
        }
        // 没找到
        return null;
    }

    /**
     * 删除所有值为 value 的节点
     */
    public void removeAllValue(int value) {
        // 判空
        if (head == null) return;

        ListNode prev = head;
        ListNode cur = head.next;

        while (cur != null) {
            // 判断 cur.val 是否和 value 相等
            if (cur.val == value) {
                // 相等则让 prev.next 指向 cur.next
                prev.next = cur.next;
            } else {
                // prev 移动到 cur 的位置
                prev = cur;
            }
            // cur 移动到 cur.next
            cur = cur.next;
        }
        // 判断第一个节点的值是否和 value 相等
        if (head.val == value) {
            head = head.next;
        }
    }

    /**
     * 获取单链表的长度
     */
    public int size() {
        ListNode cur = head;
        int count = 0;

        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    /**
     * 清空单链表
     */
    public void clear() {
        // 把 head 置为 null
        this.head = null;
    }

    /**
     * 打印数组中的元素
     */
    public void display() {
        ListNode cur = head;

        while (cur != null) {
            System.out.print(cur.val + " ");
            // 指向下一个值
            cur = cur.next;
        }
        System.out.println();
    }
}

IndexOFBoundException类:

java 复制代码
public class IndexOFBoundException extends RuntimeException {
    public IndexOFBoundException() {
    }

    public IndexOFBoundException(String message) {
        super(message);
    }
}

Test类

java 复制代码
public class Test {
    public static void main(String[] args) {
        MySingleList mySingleList = new MySingleList();
        /*mySingleList.headInsert(1);
        mySingleList.headInsert(2);
        mySingleList.headInsert(3);*/

        //mySingleList.display();

        mySingleList.tailInsert(1);
        mySingleList.tailInsert(2);
        mySingleList.tailInsert(3);
        mySingleList.tailInsert(4);
        mySingleList.tailInsert(5);
        mySingleList.tailInsert(6);
        mySingleList.display();

        mySingleList.randomInsert(0, 111);
        mySingleList.randomInsert(7, 111);
        mySingleList.randomInsert(3, 111);
        //mySingleList.randomInsert(12, 111);
        mySingleList.display();


        mySingleList.remove(2);
        mySingleList.display();

        mySingleList.removeAllValue(111);
        mySingleList.display();

    }
}
相关推荐
kkeeper~9 小时前
0基础C语言积跬步之数据在内存中的存储
c语言·数据结构·算法
2401_868534789 小时前
论企业网络设计
数据结构
wabs66610 小时前
关于贪心算法的一些自我总结【力扣45.跳跃游戏II】【灵感来源:代码随想录】
算法·贪心算法·复盘
2401_8769641310 小时前
【湖北专升本】2026湖北专升本真题PDF+备考资料汇总
数据结构·人工智能·经验分享·深度学习·算法·计算机视觉
嗝o゚11 小时前
CANN GE 算子融合——融合算法与调度策略
算法·昇腾·cann·ge
小江的记录本11 小时前
【JVM虚拟机】垃圾回收GC:垃圾回收算法:标记-清除、标记-复制、标记-整理、分代收集(附《思维导图》+《面试高频考点清单》)
java·jvm·后端·python·算法·安全·面试
Ulyanov12 小时前
用声明式语法重新定义Python桌面UI:QML+PySide6现代开发入门(一)
开发语言·python·算法·ui·系统仿真·雷达电子对抗仿真
数据科学小丫12 小时前
特征工程处理
人工智能·算法·机器学习
z落落13 小时前
C#参数区别
java·算法·c#
c2385614 小时前
vector(下)
数据结构·算法