LinkedList的顶级理解

目录

1.LinkedList的介绍

LinkedList的结构

2.LinkedList的模拟实现

2.1创建双链表 

2.2头插法

2.3尾插法

2.4任意位置插入

2.5查找关键字

2.6链表长度

2.7遍历链表

2.8删除第一次出现关键字为key的节点

2.9删除所有值为key的节点

2.10清空链表

2.11完整代码

3.LinkedList的使用 

3.1LinkedList的构造

3.2LinkedList的其他常用方法介绍 

 3.3LinkedList的遍历

3.4ArrayList和LinkedList的区别


1.LinkedList的介绍

LinkedList的底层是双向链表结构,元素存储在单独的节点中,通过引用将节点连接起来了。

如果对双向链表或链表不太清晰,可以先看看本博主写的有关链表的文章。

链表的顶级理解_WHabcwu的博客-CSDN博客

在集合框架中,LinkedList也实现了List接口,具体如下:

注意: 

1. LinkedList 实现了 List 接口
2. LinkedList 的底层使用了双向链表
3. LinkedList 没有实现 RandomAccess 接口,因此 LinkedList 不支持随机访问
4. LinkedList 的任意位置插入和删除元素时效率比较高,时间复杂度为 O(1)
5. LinkedList 比较适合任意位置插入的场景

 

LinkedList的结构

 

  • 前驱节点:用于存储前一节点的位置,用prev表示
  • 后继节点:用于储存下一节点的位置,用next表示
  • 所需要储存的数据,用val表示
  • 头节点:用head表示
  • 尾节点:用last表示

2.LinkedList的模拟实现

无非是增删查改,在某位置的插入与删除,对数据内容进行管理和操作。

具体实现内容:

(1)创建双链表

(2)头插法

(3)尾插法

(4)任意位置插入

(5)查找关键字

(6)链表长度

(7)遍历链表

(8)删除第一次出现关键字为key的节点

(9)删除所有值为key的节点

(10)清空链表

2.1创建双链表 

public class MyLinkedList {static class ListNode {public int val;public ListNode prev;//前驱public ListNode next;//后继public ListNode(int val) {this.val = val;}}public ListNode head;//头节点public ListNode last;//尾节点
}

2.2头插法

(1)首先判断头节点是否为null若为null,则该节点就是头节点,也是尾节点.

(2)头节点不为null,将原先head的前驱节点指向新增节点位置,新增节点后驱节点指向head节点的位置。

(3)head指向新增节点位置。

    //插法 O(1)public void addFirst(int data){ListNode node = new ListNode(data);if(head == null) {head = node;last = node;}else {node.next = head;head.prev = node;head = node;}}

2.3尾插法

与头插法大同小异

    //尾插法 O(1)public void addLast(int data){ListNode node = new ListNode(data);if(head == null) {head = node;last = node;}else {last.next = node;node.prev = last;last = node;}}

2.4任意位置插入

需要插入的位置必须为合法,如果不合法,我们会抛出一个异常进行提醒

public class ListIndexOutOfException extends RuntimeException{public ListIndexOutOfException() {}public ListIndexOutOfException(String message) {super(message);}
}

任意位置插入,我们可以分为种情况,插在开头,插在结尾,插在中间

    //任意位置插入,第一个数据节点为0号下标public void addIndex(int index,int data){if(index < 0 || index > size()) {throw new ListIndexOutOfException("违规数据");}if(index == 0) {addFirst(data);return;}if(index == size()) {addLast(data);return;}ListNode cur = findIndex(index);//ListNode node = new ListNode(data);cur.prev.next = node;node.next = cur;node.prev = cur.prev;cur.prev = node;}

2.5查找关键字

直接遍历查找即可

    //查找是否包含关键字key是否在单链表当中public boolean contains(int key){ListNode cur = head;while (cur != null) {if(cur.val == key) {return true;}cur = cur.next;}return false;}

2.6链表长度

用一个len变量进行记录,遍历链表

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

2.7遍历链表

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

2.8删除第一次出现关键字为key的节点

(1)一个节点都没有
(2)删除数据在第一个
(3)没有你要删除的数据
(4)有你要删除的数据且不是第一个

(5)删除数据最后一个

 找到就return;

    //删除第一次出现关键字为key的节点public void remove(int key){ListNode cur = head;while (cur != null) {//开始删除了if(cur.val == key) {//1. 删除的是头节点if(cur == head) {head = head.next;//只有一个节点if(head != null) {head.prev = null;}}else {//中间  尾巴cur.prev.next = cur.next;//不是尾巴节点if(cur.next != null) {cur.next.prev = cur.prev;}else {//是尾巴节点last = last.prev;}}return;}cur = cur.next;}}

2.9删除所有值为key的节点

与删除第一次出现关键字为key的节点几乎是一模一样的;

我们只需要遍历完就好,只需要return删掉就好。

    //删除所有值为key的节点public void removeAllKey(int key){ListNode cur = head;while (cur != null) {//开始删除了if(cur.val == key) {//1. 删除的是头节点if(cur == head) {head = head.next;//只有一个节点if(head != null) {head.prev = null;}}else {//中间  尾巴cur.prev.next = cur.next;//不是尾巴节点if(cur.next != null) {cur.next.prev = cur.prev;}else {//是尾巴节点last = last.prev;}}}cur = cur.next;}}

2.10清空链表

只需要遍历整个链表,将每个节点的前驱与后继节点都置为null就好

    public void clear(){ListNode cur = head;while(cur != null) {cur.prev  = null;cur = cur.next;cur.prev.next = null;}head = null;last = null;}

2.11完整代码

public class MyLinkedList {static class ListNode {public int val;public ListNode prev;//前驱public ListNode next;//后继public ListNode(int val) {this.val = val;}}public ListNode head;//头节点public ListNode last;//尾节点//头插法 O(1)public void addFirst(int data){ListNode node = new ListNode(data);if(head == null) {head = node;last = node;}else {node.next = head;head.prev = node;head = node;}}//尾插法 O(1)public void addLast(int data){ListNode node = new ListNode(data);if(head == null) {head = node;last = node;}else {last.next = node;node.prev = last;last = node;}}//任意位置插入,第一个数据节点为0号下标public void addIndex(int index,int data){if(index < 0 || index > size()) {throw new ListIndexOutOfException("违规数据");}if(index == 0) {addFirst(data);return;}if(index == size()) {addLast(data);return;}ListNode cur = findIndex(index);//ListNode node = new ListNode(data);cur.prev.next = node;node.next = cur;node.prev = cur.prev;cur.prev = node;}private ListNode findIndex(int index) {ListNode cur = head;while (index != 0) {cur = cur.next;index--;}return cur;}//查找是否包含关键字key是否在单链表当中public boolean contains(int key){ListNode cur = head;while (cur != null) {if(cur.val == key) {return true;}cur = cur.next;}return false;}//删除第一次出现关键字为key的节点public void remove(int key){ListNode cur = head;while (cur != null) {//开始删除了if(cur.val == key) {//1. 删除的是头节点if(cur == head) {head = head.next;//只有一个节点if(head != null) {head.prev = null;}}else {//中间  尾巴cur.prev.next = cur.next;//不是尾巴节点if(cur.next != null) {cur.next.prev = cur.prev;}else {//是尾巴节点last = last.prev;}}return;}cur = cur.next;}}//删除所有值为key的节点public void removeAllKey(int key){ListNode cur = head;while (cur != null) {//开始删除了if(cur.val == key) {//1. 删除的是头节点if(cur == head) {head = head.next;//只有一个节点if(head != null) {head.prev = null;}}else {//中间  尾巴cur.prev.next = cur.next;//不是尾巴节点if(cur.next != null) {cur.next.prev = cur.prev;}else {//是尾巴节点last = last.prev;}}}cur = cur.next;}}public int size(){int len = 0;ListNode cur = head;while (cur != null) {len++;cur = cur.next;}return len;}public void display(){ListNode cur = head;while (cur != null) {System.out.print(cur.val+" ");cur = cur.next;}System.out.println();}public void clear(){ListNode cur = head;while(cur != null) {cur.prev  = null;cur = cur.next;cur.prev.next = null;}head = null;last = null;}
}

3.LinkedList的使用 

3.1LinkedList的构造

3.2LinkedList的其他常用方法介绍 

 

 3.3LinkedList的遍历

    public static void main(String[] args) {LinkedList<Integer> linkedList = new LinkedList<>();linkedList.add(1);linkedList.add(2);linkedList.add(3);linkedList.add(4);linkedList.add(5);// foreach遍历for (int e:list) {System.out.print(e + " ");}System.out.println();System.out.println("=====================");// 使用迭代器遍历---正向遍历Iterator<Integer> iterator1 = linkedList.iterator();while(iterator1.hasNext()){System.out.println(iterator1.next());}System.out.println("=====================");// 使用反向迭代器---反向遍历ListIterator<Integer> iterator2 = linkedList.listIterator(linkedList.size());while (iterator2.hasPrevious()){System.out.println(iterator2.previous());}}

3.4ArrayList和LinkedList的区别

 


以上为我个人的小分享,如有问题,欢迎讨论!!! 

都看到这了,不如关注一下,给个免费的赞 

 

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

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

相关文章

①matlab的命令掌握

目录 输入命令 命名变量 保存和加载变量 使用内置的函数和常量 输入命令 1.您可以通过在命令行窗口中 MATLAB 提示符 (>>) 后输入命令 任务 使用命令 3*5 将数值 3 和 5 相乘。 答案 3*5 2.除非另有指定&#xff0c;否则 MATLAB 会将计算结果存储在一个名为 ans…

php 文字生成图片保存到本地

你可以使用PHP的GD库来生成图片并保存到本地。首先&#xff0c;你需要确保你的PHP环境已经安装了GD库。然后&#xff0c;你可以使用GD库的函数来创建一个画布&#xff0c;并在上面绘制文字。最后&#xff0c;使用imagepng或imagejpeg函数将画布保存为PNG或JPEG格式的图片文件。…

POI groupRow 折叠分组,折叠部分不显示问题

折叠组是什么&#xff1f;如图就是用POI 实现的&#xff0c;代码很简单&#xff1a;sheet.groupRow(开始行&#xff0c;结束行)即可 但是万万没想到&#xff0c;最终实现出的结果&#xff0c;合并的组&#xff0c;有一部分并没有渲染出来&#xff0c;如下图&#xff1a; 因为我…

yum install libreoffice‘ returned a non-zero

The command ‘/bin/sh -c yum install libreoffice’ returned a non-zero code: 1 1. 异常信息 Is this ok [y/d/N]: Exiting on user command Your transaction was saved, rerun it with:yum load-transaction /tmp/yum_save_tx.2023-08-28.13-42.EftXfl.yumtx The comman…

基于蜜獾算法优化的BP神经网络(预测应用) - 附代码

基于蜜獾算法优化的BP神经网络&#xff08;预测应用&#xff09; - 附代码 文章目录 基于蜜獾算法优化的BP神经网络&#xff08;预测应用&#xff09; - 附代码1.数据介绍2.蜜獾优化BP神经网络2.1 BP神经网络参数设置2.2 蜜獾算法应用 4.测试结果&#xff1a;5.Matlab代码 摘要…

软件国产化之殇

今天又看到这么一个帖子讨论一款国产化软件&#xff0c;属实给我震撼到了。 对于国产化产品&#xff0c;一直主打的都是”自研“&#xff0c;难道是我对”自研“这个词的理解有误&#xff1f; 做一个产品&#xff0c;别人开源了&#xff0c;你拿过来使用&#xff0c;你可以说…

Android——基本控件(下)(十九)

1. 菜单&#xff1a;Menu 1.1 知识点 &#xff08;1&#xff09;掌握Android中菜单的使用&#xff1b; &#xff08;2&#xff09;掌握选项菜单&#xff08;OptionsMenu&#xff09;的使用&#xff1b; &#xff08;3&#xff09;掌握上下文菜单&#xff08;ContextMenu&am…

Java doc等文件生成PDF、多个PDF合并

之前写过一遍文章是 图片生成PDF。 今天继续来对 doc等文件进行pdf合并以及多个pdf合并为一个pdf。 兄弟们&#xff0c;还是开箱即用。 1、doc生成pdf 依赖 <!-- doc生成pdf --><dependency><groupId>com.aspose</groupId><artifactId>aspose…

【会议征稿】2023智能通信与网络国际学术会议(ICN 2023)

2023智能通信与网络国际学术会议&#xff08;ICN 2023&#xff09; 2023 International Conference on Intelligent Communication and Networking (ICN2023) 2023智能通信与网络国际学术会议&#xff08;ICN 2023&#xff09;将于2023年11月10-12日在中国常州召开。ICN 2023…

Spring Boot 排除配置类的引用的方法

Spring Boot 提供的自动配置非常强大&#xff0c;某些情况下&#xff0c;自动配置的功能可能不符合我们的需求&#xff0c;需要我们自定义配置&#xff0c;这个时候就需要排除/禁用 Spring Boot 某些类的自动化配置了。 比如&#xff1a;数据源、邮件&#xff0c;这些都是提供…

设计模式——依赖倒转原则

文章目录 基本介绍应用实例依赖关系传递的三种方式和应用案例1, 接口传递&#xff0c;应用案例代码2, 构造方法传递&#xff0c;应用案例代码3, setter 方式传递&#xff0c;应用案例代码 依赖倒转原则的注意事项和细节 基本介绍 依赖倒转原则(Dependence Inversion Principle…

Vue3+TS+Vite中 vConsole 插件的使用

平时在web应用开发过程中&#xff0c;我们可以console.log去输出一些信息&#xff0c;但是在移动端&#xff0c;也就是在手机上&#xff0c;console.log的信息我们是看不到的&#xff0c;这时候就需要移动端调试工具vConsole 1. 依赖安装 npm install vconsole 或者 yarn ad…

扫雷小游戏

目录 一.扫雷小游戏 二.游戏主体一览 ​编辑 三.模块化设计扫雷游戏 3.1打印欢迎菜单 3.2创建两个二维数组 3.3棋盘稍加修改 3.4布置雷 3.5排查雷 四.游戏总体代码 4.1game.h头文件 4.2game.c函数实现源文件 4.3游戏main函数主体 五.游戏效果图 一.扫雷小游戏 这是…

Jmeter+ServerAgent

一、Jmeter 下载 https://jmeter.apache.org/download_jmeter.cgi选择Binaries二进制下载 apache-jmeter-5.6.2.tgz 修改配置文件 jmeter下的bin目录&#xff0c;打开jmeter.properties 文件 languagezh_CN启动命令 cd apache-jmeter-5.6/bin sh jmeter二、ServerAgent 监…

nginx 托管vue项目配置

server {listen 80;server_name your_domain.com;location / {root /path/to/your/vue/project;index index.html;try_files $uri $uri/ /index.html;} }奇怪的现象,在vue路由中/会跳转到/abc/def&#xff0c;但如果直接输入/abc/def会显示404&#xff0c;添加 try_files $uri…

实战 图书馆系统管理案例

config &#xff1a;敏感的配置一般都是在配置中心配置&#xff0c;比如consul或者阿波罗上面controller &#xff1a;写一些handler的&#xff0c;拿到参数要去调用service层的逻辑。&#xff08;只负责接受参数&#xff0c;怎么绑定参数&#xff0c;要去调用哪个service的&am…

Viobot输出数据说明

一.原始数据 1.ROS话题 1)相机原始图像数据 Type: sensor_msgs::Image Topic: 左目&#xff1a;/image_left 右目&#xff1a;/image_right 2&#xff09;imu数据 Type: sensor_msgs::Imu Topic: /imu 3&#xff09;TOF数据 点云数据&#xff1a; Type: sensor_msgs::P…

算法与数据结构(十)--图的入门

一.图的定义和分类 定义&#xff1a;图是由一组顶点和一组能够将两个顶点连接的边组成的。 特殊的图&#xff1a; 1.自环&#xff1a;即一条连接一个顶点和其自身的边; 2.平行边&#xff1a;连接同一对顶点的两条边&#xff1b; 图的分类&#xff1a; 按照连接两个顶点的边的…

带你速览主数据管理(MDM)的前世今生

主数据管理的历史可以追溯到很久以前&#xff0c;可以说主数据管理是生产生活的一部分。随着社会生产力和生产工具的不断发展&#xff0c;主数据和主数据管理在其中的作用不断提升&#xff0c;成为当今政府、企业和社会团队等组织管理中必不可少基础管理工作&#xff0c;同时也…

FrameBuffer 应用编程

目录 什么是FrameBufferLCD 的基础知识使用ioctl()获取屏幕参数信息使用mmap()将显示缓冲区映射到用户空间 LCD 应用编程练习之LCD 基本操作LCD 应用编程练习之显示BMP 图片BMP 图像介绍在LCD 上显示BMP 图像在开发板上测试 在LCD 上显示jpeg 图像在LCD 上显示png 图片LCD 横屏…