【数据结构——链表的深度探索】从实现到应用,保姆级攻略

news2024/9/23 19:28:49

【数据结构——链表深度探索】从实现到应用,保姆级攻略

  • 🍁1. 链表的介绍
  • 🍁2. 链表的实现
    • 🍁2.1 单向链表
      • 🍁2.1.1 size()
      • 🍁2.1.2 display()
      • 🍁2.1.3 contains(int key)
      • 🍁2.1.4 addFirst(int key),addLast(int key),addIndex(int key, int index)
      • 🍁2.1.5 remove(int key),removeAllKey(int key)
      • 🍁2.1.6 clear()
    • 🍁2.2 双向链表
      • 🍁2.2.1 addFirst(int key)
      • 🍁2.2.2 addLast(int key)
      • 🍁2.2.3 addIndex(int key, int index)
      • 🍁2.2.4 remove(int key)和removeAllKey(int key)
      • 🍁2.2.5 clear()
  • 🍁3. Java中LinkedList的使用
    • 🍁3.1 LinkedList的创建和使用
    • 🍁3.2 LinkedList的遍历
  • 🍁4. ArrayList和LinkedList的区别

🚀欢迎互三👉: 2的n次方_💎💎
🚀所属专栏:数据结构与算法学习💎💎

在这里插入图片描述

在这里插入图片描述

🍁1. 链表的介绍

链表是数据结构中一种非常重要的基础结构,它不同于数组,链表中的元素在物理存储上并不连续,而是通过指针(或引用)连接在一起。在Java中,链表的应用非常广泛,尤其是在需要动态添加或删除元素的场景中。

🍁2. 链表的实现

🍁2.1 单向链表

单链表中的每个元素都称为节点(Node),每个节点包含两个部分:一部分存储数据(value),另一部分存储指向列表中下一个节点的引用(next)。最后一个节点的next引用为null,表示链表的结束。

所以采用内部类的形式进行创建:

public class MySingleList {
    static class ListNode {
        public int value;
        public ListNode next;

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

    public ListNode head;
}

还可以创建一个IList接口,对其中的增删查改等方法进行规范,之后MySingleList对接口进行实现

public interface IList {
    void display();
    int size();
    boolean contains(int key);
    void addFirst(int key);
    void addLast(int key);
    void addIndex(int key,int index);
    void remove(int key);
    void removeAllKey(int key);
    void clear();
}

接下来就是方法的实现

🍁2.1.1 size()

返回长度:

只需要将head依次往末尾移动,并记录移动次数进行返回就可以了,当head为null时就表示已经遍历完成

    public int size() {
        ListNode cur = head;
        int cnt = 0;
        while (cur != null) {
            cnt++;
            cur = cur.next;
        }
        return cnt;
    }

🍁2.1.2 display()

遍历打印:

遍历的话需要找到头节点,接着依次往后移动,为了不该变头节点的指向,创建一个cur节点辅助遍历,同样的,结束的标志也是最后的指向不为空

public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }

🍁2.1.3 contains(int key)

判断值是否存在链表中,这里同样需要依次遍历,然后比较value的值

public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

🍁2.1.4 addFirst(int key),addLast(int key),addIndex(int key, int index)

头插:

头插比较简单,直接创建一个节点,并初始化值,指向原来的head节点,接着改为新的head节点

public void addFirst(int key) {
        ListNode node = new ListNode(key);
        node.next = head;
        head = node;
    }

尾插:

尾插就需要判断head节点是否为null,接着找到尾节点进行插入

public void addLast(int key) {
        ListNode node = new ListNode(key);
        //头结点为null,直接插入
        if (head == null) {
            head = node;
            return;
        }
        //找到尾节点进行插入
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

在指定索引插入:

在指定索引插入就更加麻烦一些,需要对传入的索引进行判断,如果是0.就调用头插的方法,如果等于链表的长度,就调用尾插的方法,如果是中间的索引,就遍历链表,找到该索引进行插入

public void addIndex(int key, int index) {
        ListNode node = new ListNode(key);
        //调用头插
        if (index == 0) {
            addFirst(key);
            return;
        }
        //调用尾插
        if (index == this.size()) {
            addLast(key);
            return;
        }
        //传入索引不合法的情况
        if (index < 0 || index > this.size()) {
            throw new IndexOutOfBoundsException();
        }
        //找到目标索引进行插入
        ListNode cur = head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        node.next = cur.next;
        cur.next = node;
    }

🍁2.1.5 remove(int key),removeAllKey(int key)

删除指定元素:

如果head为空,直接返回,如果head的value就是目标元素,就把head的下一个节点作为头结点,其他情况就根据value的值寻找目标索引,如果没找到就返回,也就是cur节点为null,找到的话把cur的引用指向cur的之后第二个节点

//根据元素找到目标索引
private ListNode findIndexofKet(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.value == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }
   
    public void remove(int key) {
    //头结点为空
        if (head == null) {
            return;
        }
        //头结点为目标元素
        if (head.value == key) {
            head = head.next;
        }
        //其他节点为目标元素
        ListNode cur = findIndexofKet(key);
        if (cur == null) {
            return;
        }
        cur.next = cur.next.next;
    }

删除所有指定元素:

需要有两个指针,同时往后遍历,删除cur节点所指元素需要将pre节点的next指向cur的next,循环判断,最后还要判断head节点是否为指定元素

public void removeAllKey(int key) {
		//头结点为null,直接返回
        if (this.head == null) {
            return;
        }
        ListNode pre = head;
        ListNode cur = head.next;
        //循环删除
        while (cur != null) {
            if (cur.value == key) {
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = cur;
                cur = cur.next;
            }
        }
        //判断头结点
        if (head.value == key) {
            head = head.next;
        }
    }

🍁2.1.6 clear()

清空链表:

清空链表只需要遍历链表所有节点,将每一个节点置为null即可,因为是从头结点开始,如果直接将head置为null,后续再找head.next就会报错,所以还需要用一个中间变量cur辅助遍历

public void clear() {
        ListNode cur = head;
        while (cur != null) {
            //创建变量,解决空指针异常
            ListNode curn = cur.next;
            cur = null;
            cur = curn.next;
        }
        head = null;
    }

🍁2.2 双向链表

双向链表有两个指针域,一个指向前一个节点,一个指向后一个节点
在这里插入图片描述

public class MyLinkedList implements IList {
    static class TListNode {
        public int value;
        TListNode pre;
        TListNode next;

        public TListNode(int value) {
            this.value = value;
        }
    }

    public TListNode head;
    public TListNode last;
}

双向链表的size() ,display(),contains(int key)和单向链表是一样的,下面来实现其他的方法

🍁2.2.1 addFirst(int key)

头插:

在这里插入图片描述

public void addFirst(int key) {
        TListNode node = new TListNode(key);
        if (head == null) {
            head = last = node;
        } else {
            node.next = head;
            head.pre = node;
            head = node;
        }
    }

🍁2.2.2 addLast(int key)

尾插:

public void addLast(int key) {
        TListNode node = new TListNode(key);
        if (head == null) {
            head = last = node;
        } else {
            last.next = node;
            node.pre = last;
            last = last.next;
        }
    }

🍁2.2.3 addIndex(int key, int index)

指定位置插入:

public void addIndex(int key, int index) {
        TListNode node = new TListNode(key);
        if(index < 0 || index > size()) return;
        if (index == 0) {
            addFirst(key);
            return;
        }
        if (index == size()) {
            addLast(key);
        }
        if (index > 0 && index < size()) {
            TListNode cur = head;
            //可以直接到indext的位置,因为双向链表可以找到前一个节点
            while (index-- != 0) {
                cur = cur.next;
            }
            node.next = cur;
            cur.pre.next = node;
            node.pre = cur.pre;
            cur.pre = node;
        }
    }

需要修改四个位置,把要插入的节点node的next 指向cur,再把cur.pre的next指向node,此时节点的next都有了指向,接着node的pre指向cur.pre节点,cur的pre再指向node,此时就完成了插入
在这里插入图片描述

🍁2.2.4 remove(int key)和removeAllKey(int key)

首先找到要删除的值的索引

private TListNode findIndexofKet(int key) {
        TListNode cur = head;
        while (cur != null) {
            if (cur.value == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

删除的时候还要考虑是否为头结点和尾节点

public void remove(int key) {
        TListNode cur = findIndexofKet(key);
        if(cur == null){
            return;
        }
        //头节点的情况
        if(cur == head){
            head = cur.next;
            //只有一个节点时,指向next后head为null所以当head!=空时才能把pre置为null
            if (head != null) {
                head.pre = null;
            }
        }else{
            cur.pre.next = cur.next;
            //尾节点的情况
            if(cur.next == null){
                last = last.pre;
            }else{
                cur.next.pre = cur.pre;
            }
        }
    }

相比于单向链表,双向链表的删除所有指定元素就非常简单了,只需要在原来删除一个的基础上稍加修改就可以了

public void removeAllKey(int key) {
        TListNode cur = head;
        while (cur != null) {
            if (cur.value == key) {
                if (cur == head) {
                    head = cur.next;
                    if (head != null) {
                        head.pre = null;
                    }
                } else {
                    cur.pre.next = cur.next;
                    if (cur.next == null) {
                        last = last.pre;
                    } else {
                        cur.next.pre = cur.pre;
                    }
                }
            }
            cur = cur.next;
        }
    }

🍁2.2.5 clear()

清空链表还是依次遍历每一个节点,把每一个节点都置为null,最后把head和last也置为null

public void clear() {
        TListNode cur = head;
        while(cur.next!=null){
            TListNode curn = cur;
            curn.pre = null;
            curn.next = null;
            cur = curn;
        }
        head = last = null;
    }

🍁3. Java中LinkedList的使用

🍁3.1 LinkedList的创建和使用

在上一篇数据结构ArrayList的讲解中已经简单提到过👉点我看回顾,集合的一些基本框架,LinkedList也实现了List接口,所以也可以通过接口创建对象,也可以使用List接口中的方法

public class Demo {
    public static void main(String[] args) {
        LinkedList<Integer> list1 = new LinkedList<>();
        List<Integer> list2 = new LinkedList<>();
        list1.add(1);
        list1.add(2);
        System.out.println(list1);
        list2.add(1);
        list2.add(3);
        System.out.println(list2);
    }
}

在这里插入图片描述
可以直接对LinkedList的对象进行打印,也就是说LinkedList重写了toSting方法
在这里插入图片描述
这些都是LinkedList中独有的方法,这里就不列举使用了,

🍁3.2 LinkedList的遍历

LinkedList的遍历和ArrayList的遍历方式一样,在上一篇介绍了五种遍历方式,这次再简单回顾一下

public class Demo {
    public static void main(String[] args) {
        LinkedList<Integer> list1 = new LinkedList<>();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        list1.add(4);
        //迭代器 ListIterator
        ListIterator<Integer> lit = list1.listIterator();
        while(lit.hasNext()){
            System.out.print(lit.next() + " ");
        }
        System.out.println();
        //Iterator
        Iterator<Integer> it = list1.iterator();
        while(it.hasNext()){
            System.out.print(it.next() + " ");
        }
        System.out.println();
        //增强for
        for(Integer l : list1){
            System.out.print(l + " ");
        }
        System.out.println();
        //普通for
        for(int i = 0;i < list1.size();i++){
            System.out.print(list1.get(i) + " ");
        }
        System.out.println();
        //lambda表达式
        list1.forEach(e -> {
            System.out.print(e + " ");
        });
        System.out.println();
    }
}

🍁4. ArrayList和LinkedList的区别

在这里插入图片描述
ArrayList底层是一个动态数组
LinkedList底层是双向链表
ArrayList:访问元素的时间复杂度为 O(1)(直接通过索引)。
LinkedList:访问元素的时间复杂度为 O(n)(需要从头或尾开始遍历到目标位置)。
ArrayList:
在末尾添加元素的时间复杂度为 O(1)
在中间插入或删除元素时,时间复杂度为 O(n),因为需要移动其他元素以保持连续的内存块。
LinkedList:
在任意位置添加或删除元素的时间复杂度为 O(1),只需改变前后节点的指针(需要先找到目标位置,时间复杂度为 O(n))。

使用场景:
ArrayList:
适合频繁读取、随机访问元素的场景。
如需要大量顺序读写、索引访问操作。
LinkedList:
适合频繁插入、删除元素的场景,尤其是在列表中间进行操作。
如需要频繁的增删操作,但不常用到随机访问。

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1915479.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

本地部署,强大的面部修复与增强网络CodeFormer

目录 什么是 CodeFormer&#xff1f; 技术原理 主要功能 应用场景 本地部署 运行结果 结语 Tip&#xff1a; 在图像处理和计算机视觉领域&#xff0c;面部修复和增强一直是一个备受关注的研究方向。近年来&#xff0c;深度学习技术的飞速发展为这一领域带来了诸多突破性…

C++ 语法习题(3)

字符串 1.字符串长度 给定一行长度不超过 100 的非空字符串&#xff0c;请你求出它的具体长度。 输入格式 输入一行&#xff0c;表示一个字符串。注意字符串中可能包含空格。 输出格式 输出一个整数&#xff0c;表示它的长度。 数据范围 1≤字符串长度≤100 字符串末尾…

2024学生党蓝牙耳机什么牌子好?品牌高性价比蓝牙耳机推荐

2024年&#xff0c;对于追求性价比和品质的学生党来说&#xff0c;选择一款合适的蓝牙耳机是提升学习和生活品质的重要一环。面对市场上琳琅满目的蓝牙耳机产品&#xff0c;2024学生党蓝牙耳机什么牌子好&#xff1f;如何找到既满足音质需求又具备高性价比的款式呢&#xff1f;…

Odoo免费开源ERP如何处理汽车零部件企业的OE编码问题

业务背景 汽车零部件企业在每个汽配零件都有OE编号&#xff0c;即原厂编号&#xff0c;Original Equipment Number。一个配件&#xff0c;可能可以在多个车型上使用&#xff0c;对应的&#xff0c;就有多个可兼容的OE编号。 客户下单时候&#xff0c;直接报OE编号&#xff0c…

MT3054 搭积木

1.思路&#xff1a; 把二维矩阵转化成一维编号&#xff0c;之后将编号使用并查集&#xff0c;看最后是否在同一个集合中即可。 2.代码&#xff1a; #include <bits/stdc.h> using namespace std; const int N 1e3 10; int n, m, cnt, root; int fa[N * N]; int dx[…

[机器学习]-人工智能对程序员的深远影响——案例分析

机器学习和人工智能对未来程序员的深远影响 目录 机器学习和人工智能对未来程序员的深远影响1. **自动化编码任务**1.1 代码生成1.2 自动调试1.3 测试自动化 2. **提升开发效率**2.1 智能建议2.2 项目管理 3. **改变编程范式**3.1 数据驱动开发 4. **职业发展的新机遇**4.1 AI工…

【代码随想录】【算法训练营】【第63天】 [卡码53]寻宝

前言 思路及算法思维&#xff0c;指路 代码随想录。 题目来自 LeetCode。 day 63&#xff0c;周二&#xff0c;ding~ 题目详情 [卡码53] 寻宝 题目描述 卡码53 寻宝 解题思路 前提&#xff1a; 思路&#xff1a; 重点&#xff1a; 代码实现 C语言 prim算法 kruskal…

UDP协议介绍和作用

什么是UDP? UDP是User Datagram Protocol的简称&#xff0c;中文名是用户数据报协议&#xff0c;是OSI参考模型中的传输层协议&#xff0c;它是一种无连接的传输层协议&#xff0c;提供面向事务的简单不可靠信息传送服务。 UDP的正式规范是IETF RFC768。UDP在IP报文的协议号是…

java数组之线性查找、二分法查找

一、线性查找 思想&#xff1a;如果想在一个数组中查找是否有某个元素&#xff0c;最容易想到的办法就是遍历数组&#xff0c;将数组中元素与想要查找的元素逐个对比&#xff0c;如果相等表示找到了&#xff0c;如果不等&#xff0c;则表示没找到。这就是线性查找的思想。 案例…

Chat2DB:AI引领下的全链路数据库管理新纪元

一、引言 随着数据驱动决策成为现代企业和组织的核心竞争力&#xff0c;数据库管理工具的重要性日益凸显。然而&#xff0c;传统的数据库管理工具往往存在操作复杂、功能单一、不支持多类型数据库管理等问题&#xff0c;限制了数据的有效利用。为了打破这一局面&#xff0c;Ch…

东方通Tongweb发布vue前端

一、前端包中添加文件 1、解压vue打包文件 以dist.zip为例&#xff0c;解压之后得到dist文件夹&#xff0c;进入dist文件夹&#xff0c;新建WEB-INF文件夹&#xff0c;进入WEB-INF文件夹&#xff0c;新建web.xml文件&#xff0c; 打开web.xml文件&#xff0c;输入以下内容 …

代码随想录算法训练营第四十九天| 647. 回文子串、 516.最长回文子序列

647. 回文子串 题目链接&#xff1a;647. 回文子串 文档讲解&#xff1a;代码随想录 状态&#xff1a;不会 思路&#xff1a; dp[i][j] 表示字符串 s 从索引 i 到索引 j 这一段子串是否为回文子串。 当s[i]与s[j]不相等&#xff0c;那没啥好说的了&#xff0c;dp[i][j]一定是fa…

Chromium编译指南2024 Linux篇-编译Chromium(五)

1.引言 在完成环境配置之后&#xff0c;我们需要开始实际的编译工作。编译 Chromium 是一个相对复杂且耗时的过程&#xff0c;尤其是第一次编译时。为了保证顺利完成&#xff0c;我们将使用 GN 和 Ninja 工具来生成和管理构建文件。接下来&#xff0c;我们会详细介绍如何生成构…

Cesium中实现全球体积云效果的一种方案

原生 Cesium 提供了一种积云的效果&#xff0c;云的物理特征和渲染性能都还不错&#xff0c;这种方案适合表达小范围相对离散的云朵&#xff0c;但是用来实现全球范围下相对连续、柔和渐变的云层比较困难。本文在体渲染的基础上&#xff0c;参考了开源社区中 shadertoy 和 thre…

软件架构之软件架构概述及质量属性

软件架构之软件架构概述及质量属性 第 9 章&#xff1a;软件架构设计9.1 软件架构概述9.1.1 软件架构的定义9.1.2 软件架构的重要性9.1.3 架构的模型 9.2 架构需求与软件质量属性9.2.1 软件质量属性9.2.2 6 个质量属性及实现 第 9 章&#xff1a;软件架构设计 像学写文章一样&…

java通过poi-tl导出word实战详细步骤

文章目录 与其他模版引擎对比1.引入maven依赖包2.新建Word文档exportWprd.docx模版3.编写导出word接口代码4.导出成果 poi-tl是一个基于Apache POI的Word模板引擎&#xff0c;也是一个免费开源的Java类库&#xff0c;你可以非常方便的加入到你的项目中&#xff0c;并且拥有着让…

模板语法之插值语法{{}}——01

<主要研究&#xff1a;{{ 这里可以写什么}} 1.在data中声明的变量函数都可以 2.常量 3.只要是合法的JavaScript的表达式&#xff0c;都可以 4. 模板表达式都被放在沙盒中&#xff0c;只能访问全局变量的一个白名单&#xff0c;如 Math 和 Date <body> <div i…

进程间的通信--管道

文章目录 一、进程通信的介绍1.1进程间为什么需要通信1.2进程如何通信 二、管道2.1匿名管道2.1.1文件描述符理解管道2.1.2接口使用2.1.3管道的4种情况2.1.4管道的五种特征 2.2管道的使用场景2.2.1命令行中的管道2.2.2进程池 2.命名管道2.1.1原理2.2.2接口2.2.3代码实例 一、进程…

网络编程:各协议头(数据报格式)

一、mac头 二、ip头 protocol——tcp/udp &#xff08;7&#xff09;TTL——生存时间 三、tcp头 四、udp头

最新2023年行政区划、路网、土壤质地矢量数据

行政区划矢量数据是指用矢量格式表示的地理信息系统&#xff08;GIS&#xff09;数据&#xff0c;其中包含了行政区域的边界信息&#xff0c;如国家、省份、城市、区县、乡镇甚至村级的界限。这些数据通常以点、线、面的几何图形来表示具体的地理实体&#xff0c;并且每个实体都…