博客
关于我
链表的实现
阅读量:638 次
发布时间:2019-03-14

本文共 4226 字,大约阅读时间需要 14 分钟。

链表

概念

链表是一种基于物理存储单元的非连续数据结构。通过引用链接,数据元素的逻辑顺序被实现。


基本方法

  • void addFirst()

    通过头插法插入新节点到链表头部。

  • void addLast()

    通过尾插法插入新节点到链表尾部。

  • void addIndex(int index, int data)

    根据指定索引插入新节点,索引为0号起始。

  • boolean contains(int key)

    检查链表中是否包含指定键的节点。

  • void remove(int key)

    删除第一次出现的指定键的节点。

  • void removeAll(int key)

    删除所有包含指定键的节点。


  • 实现代码

    class ListNode {    public int val;    public ListNode next;    public ListNode(int val) {        this.val = val;    }}public class MyLinkedList {    public ListNode head;    public MyLinkedList() {    }    // 头插法    public void addFirst(int data) {        ListNode node = new ListNode(data);        if (head == null) {            head = node;        } else {            node.next = head;            head = node;        }    }    // 尾插法    public void addLast(int data) {        ListNode node = new ListNode(data);        if (head == null) {            head = node;        } else {            ListNode cur = head;            while (cur.next != null) {                cur = cur.next;            }            cur.next = node;        }    }    // 指定位置插入    public void addIndex(int index, int data) {        if (index < 0 || index > size()) {            System.out.println("插入位置无效!"                            + " 请确保索引在0到" + size() + "之间");            return;        }        ListNode node = new ListNode(data);        if (head == null) {            return;        }        if (index == 0) {            addFirst(data);        } else if (index == size() - 1) {            addLast(data);        } else {            ListNode prev = searchPrev(index);            node.next = prev.next;            prev.next = node;        }    }    // 判断是否包含元素    public boolean contains(int key) {        ListNode cur = head;        if (head == null) {            return false;        }        while (cur != null) {            if (cur.val == key) {                return true;            }            cur = cur.next;        }        return false;    }    // 删第一次出现的key    public void remove(int key) {        if (head.val == key) {            head = head.next;        } else {            ListNode prev = searchPrev2(key);            if (prev == null) {                return;            }            while (head != null) {                if (head.val == key) {                    prev.next = head.next;                    head = head.next;                } else {                    head = head.next;                }            }        }    }    // 删所有key    public void removeAllKey(int key) {        if (head == null) {            return;        }        if (head.val == key) {            head = head.next;        }        ListNode cur = head;        while (cur != null) {            if (cur.val == key) {                ListNode prev = searchPrev2(key);                if (prev != null) {                    prev.next = cur.next;                }            }            cur = cur.next;        }    }    public int size() {        int count = 0;        ListNode cur = head;        while (cur != null) {            count++;            cur = cur.next;        }        return count;    }    // 打印链表内容    public void display() {        if (head == null) {            System.out.println("链表为空!");            return;        }        ListNode cur = head;        while (cur != null) {            System.out.print(cur.val + " ");            cur = cur.next;        }        System.out.println();    }    // 清空链表    public void clear() {        head = null;    }    public static void main(String[] args) {        MyLinkedList myLinkedList = new MyLinkedList();        myLinkedList.addFirst(33);        myLinkedList.addFirst(33);        myLinkedList.addFirst(22);        myLinkedList.addFirst(44);        myLinkedList.addFirst(33);        myLinkedList.display();        myLinkedList.addLast(88);        myLinkedList.display();        System.out.println(myLinkedList.contains(111));        myLinkedList.remove(33);        myLinkedList.display();        myLinkedList.removeAllKey(33);        myLinkedList.display();        myLinkedList.addIndex(7, 99);        myLinkedList.display();        System.out.println(myLinkedList.size());    }}

    主要优化点

    • 使用更简洁的语言风格,避免AI生成的感觉。
    • 去除了不必要的语法注释,保持代码规范一致。
    • 统一了代码中的空格和格式,提升可读性。
    • 删除了无关的输出语句,提供更干净的阅读体验。
    • 保持了核心功能不变,但在语言表达上更贴近人类思维方式。

    转载地址:http://bogoz.baihongyu.com/

    你可能感兴趣的文章
    MTK Android 如何获取系统权限
    查看>>
    MySQL - 4种基本索引、聚簇索引和非聚索引、索引失效情况、SQL 优化
    查看>>
    MySQL - ERROR 1406
    查看>>
    mysql - 视图
    查看>>
    MySQL - 解读MySQL事务与锁机制
    查看>>
    MTTR、MTBF、MTTF的大白话理解
    查看>>
    mt_rand
    查看>>
    mysql -存储过程
    查看>>
    mysql /*! 50100 ... */ 条件编译
    查看>>
    mysql 1045解决方法
    查看>>
    mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
    查看>>
    mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
    查看>>
    mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
    查看>>
    mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
    查看>>
    mui折叠面板点击事件跳转
    查看>>
    MySQL 8 公用表表达式(CTE)—— WITH关键字深入用法
    查看>>
    mysql 8 远程方位_mysql 8 远程连接注意事项
    查看>>
    MUI框架里的ajax的三种方法
    查看>>
    MySQL 8.0 恢复孤立文件每表ibd文件
    查看>>
    Mysql 8.0 新特性
    查看>>