【Java数据结构】LinkedList与链表

认识LinkedList

        LinkedList就是一个链表,它也是实现List接口的一个类。LinkedList就是通过next引用将所有的结点链接起来,所以不需要数组。LinkedList也是以泛型的方法实现的,所以使用这个类都需要实例化对象。

        链表分为很多种,比较常用的就两种:单链表(单向、不带头、非循环)和双链表(双向、不带头、非循环),后面会模拟实现。下面是顺序表和链表的区别:

模拟实现LinkedList

单链表

        首先需要创建结点,但是它比顺序表多了一个next引用,可以通过next引用来访问下一个结点,不再需要通过连续地址访问,首先先创建结点这个类, 然后再实现增删查改等这些方法:

链表长度、遍历链表、头插法、尾插法、任意位置插入、查找关键字key是否在链表中 、删除第一次出现关键字为key的节点、删除所有值为key的节点、清空。

public class MySingle {static class ListNode{private int val;private ListNode next;public ListNode(int val){this.val = val;}}private ListNode head;//头插法public void addFirst(int data){ListNode node = new ListNode(data);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()){throw new IndexOutOfException("位置不合法!");}if (index == 0){addFirst(data);return;}ListNode node = new ListNode(data);ListNode cur = head;while (index - 1 != 0){cur = cur.next;index--;}//将index位置前后两个节点和新结点联系起来node.next = cur.next;cur.next = node;}//查找是否包含关键字key是否在单链表当中public boolean contains(int key){ListNode cur = head;while (cur != null){if (cur.val == key){return true;}}return false;}public ListNode keyPre(int key){ListNode cur = head;while (cur.next != null){if (cur.next.val == key){return cur;}cur = cur.next;}return null;}//删除第一次出现关键字为key的节点public void remove(int key){if (head == null){return;}if (head.val == key){head = head.next;return;}//找到key结点的前一个结点ListNode pre = keyPre(key);ListNode del = pre.next;pre.next = del.next;}//删除所有值为key的节点public void removeAllKey(int key){ListNode pre = head;ListNode cur = head.next;while (cur != null){if (cur.val == key){pre.next = cur.next;cur = cur.next;}else{pre = pre.next;cur = cur.next;}}if (head.val == key){head = head.next;}}//得到单链表的长度public int size(){int count = 0;ListNode cur = head;while (cur != null){count++;cur = cur.next;}return count;}public void display(){ListNode cur = head;while (cur != null){System.out.print(cur.val+"  ");cur = cur.next;}System.out.println();}
}

双链表(LinkedList)

        LinkedList其实就是一个双链表, 它既可以访问前驱又可以访问后继,所以可以快速插入和删除。下面是LinkedList模拟实现,比较难的就是插入和删除:

public class MyLinkedList {static class ListNode{private int val;private ListNode prev;private ListNode next;public ListNode(int val){this.val = val;}}public ListNode head;public ListNode tail;//头插法public void addFirst(int data){ListNode node = new ListNode(data);if (head == null){head = node;tail = node;}else {head.prev = node;node.next = head;head = node;}}//尾插法public void addLast(int data){ListNode node = new ListNode(data);if (tail == null){head = node;tail = node;}else{tail.next = node;node.prev = tail;tail = node;//tail = tail.next;}}//任意位置插入,第一个数据节点为0号下标public void addIndex(int index,int data){if (index < 0 || index > size()){throw new IndexOutofBoundException(index+"位置不合理!");}ListNode node = new ListNode(data);ListNode cur = head;while (index > 0){cur = cur.next;index--;}node.next = cur;cur.prev.next = node;node.prev = cur.prev;cur.prev = node;}//查找是否包含关键字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){if (head == null){return ;}ListNode cur = head;while(cur != null ) {if (cur.val == key){if (cur.next == null && cur.prev == null){head = null;tail = null;return;}if (cur.prev == null) {head = cur.next;cur.next.prev = null;return;}if(cur.next == null){tail = cur.prev;cur.prev.next = null;return;}cur.prev.next = cur.next;cur.next.prev = cur.prev;return;}else{cur = cur.next;}}}//删除所有值为key的节点public void removeAllKey(int key){if (head == null){return ;}ListNode cur = head;while(cur != null ) {if (cur.val == key){if (cur.next == null && cur.prev == null){head = null;tail = null;}else if (cur.prev == null) {head = cur.next;cur.next.prev = null;}else if(cur.next == null){tail = cur.prev;cur.prev.next = null;}else {cur.prev.next = cur.next;cur.next.prev = cur.prev;}cur = cur.next;}else{cur = cur.next;}}}//得到单链表的长度public int size(){ListNode cur = head;int size = 0;while (cur != null){size++;cur = cur.next;}return size;}//清空public void clear(){ListNode cur = head;while(cur != null){ListNode curNext = cur.next;cur.prev = null;cur.next = null;cur = curNext;}head = null;tail = null;}//遍历public void display(){ListNode cur = head;while (cur != null){System.out.print(cur.val+"   ");cur = cur.next;}System.out.println();}
}

LinkedList的创建

构造一个对象,可以无参构造(较为常用,在其里初始化一个数组)。 

LinkedList的遍历

        遍历的方法和ArrayList里一样,三种:for循环、增强for循环、迭代器。这两个遍历方法都是一样的,就不重复叙述。https://blog.csdn.net/2402_84815218/article/details/144038207?spm=1001.2014.3001.5502

LinkedList常用方法

 这些方法和ArrayList中的方法差不多都一样,只是实现的过程不一样。这里我就举几个例子:

    public static void main(String[] args) {LinkedList<Integer> list = new LinkedList<>();list.add(12);//尾插进入list.add(23);System.out.println(list);//【12,23】
//        list.add(3, 100);//插入到第index位置,但是该list中没有第三个位置
//        System.out.println(list);System.out.println(list.get(1));//得到1位置元素  23list.set(1,100);//更新1位置的元素System.out.println(list.get(1));// 100list.remove(1);//删除1位置元素list.clear();//清空链表System.out.println(list);//【】}

ArrayList与LinkedList的区别

        简而言之就是LinkedList的插入和删除的复杂度高,而ArrayList的可能需要移动n次数据,所以应用根据应用场景来判断使用哪一种。         

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

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

相关文章

《一文读懂卷积网络CNN:原理、模型与应用全解析》

《一文读懂卷积网络CNN&#xff1a;原理、模型与应用全解析》 一、CNN 基本原理大揭秘&#xff08;一&#xff09;从人类视觉到 CNN 灵感&#xff08;二&#xff09;核心组件详解 二、经典 CNN 模型巡礼&#xff08;一&#xff09;LeNet-5&#xff1a;开山鼻祖&#xff08;二&a…

教育元宇宙的优势与核心功能解析

随着科技的飞速发展&#xff0c;教育领域正迎来一场前所未有的变革。教育元宇宙作为新兴的教育形态&#xff0c;以其独特的优势和丰富的功能&#xff0c;正在逐步改变我们的学习方式。本文将深入探讨教育元宇宙的优势以及其核心功能&#xff0c;为您揭示这一未来教育的新趋势。…

openGauss与GaussDB系统架构对比

openGauss与GaussDB系统架构对比 系统架构对比openGauss架构GaussDB架构 GaussDB集群管理组件 系统架构对比 openGauss架构 openGauss是集中式数据库系统&#xff0c;业务数据存储在单个物理节点上&#xff0c;数据访问任务被推送到服务节点执行&#xff0c;通过服务器的高并…

idea 8年使用整理

文章目录 前言idea 8年使用整理1. 覆盖application配置2. 启动的时候设置编辑空间大小&#xff0c;并忽略最大空间3. 查询类的关系4. 查看这个方法的引用关系5. 查看方法的调用关系5.1. 查看被调用关系5.2. 查看调用关系 6. 方法分隔线7. 选择快捷键类型8. 代码预览插件9. JReb…

C++ OCR 文字识别

一.引言 文字识别&#xff0c;也称为光学字符识别&#xff08;Optical Character Recognition, OCR&#xff09;&#xff0c;是一种将不同形式的文档&#xff08;如扫描的纸质文档、PDF文件或数字相机拍摄的图片&#xff09;中的文字转换成可编辑和可搜索的数据的技术。随着技…

Unity中UGUI的Button动态绑定引用问题

Unity中UGUI的Button动态绑定引用问题 问题代码修改代码如下总结 问题代码 Button动态绑定几个连续的按钮事件时使用for循环的i做按钮的id发现按钮点击对应不上。如下代码 for (int i 0; i < 10; i) {btn[i].onClick.AddListener(() >{Click(i);}); }/// <summary&…

SQL中的窗口函数

1.窗口函数简介 窗口函数是SQL中的一项高级特性&#xff0c;用于在不改变查询结果集行数的情况下&#xff0c;对每一行执行聚合计算或者其他复杂的计算&#xff0c;也就是说窗口函数可以跨行计算&#xff0c;可以扫描所有的行&#xff0c;并把结果填到每一行中。这些函数通常与…

SpringBoot(Ⅱ)——@SpringBootApplication注解+自动装配原理+约定大于配置

1. SpringBootApplication注解 SpringBootApplication标注在某个类上说明这个类是SpringBoot的主配置类&#xff0c;SpringBoot就通过运行这个类的main方法来启动SpringBoot应用&#xff1b; 并且Configuration注解中也有Component注解&#xff0c;所以这个主启动类/主配置类…

音视频入门知识(二)、图像篇

⭐二、图像篇 视频基本要素&#xff1a;宽、高、帧率、编码方式、码率、分辨率 ​ 其中码率的计算&#xff1a;码率(kbps)&#xff1d;文件大小(KB)&#xff0a;8&#xff0f;时间(秒)&#xff0c;即码率和视频文件大小成正比 YUV和RGB可相互转换 ★YUV&#xff08;原始数据&am…

信息安全管理:开发测试安全管理checklist

开发测试安全管理包括安全需求、安全设计、安全编码测试、系统部署上线、商用及开源软件使用、开发测试资源安全六个部分的内容。 01.安全需求 在信息系统需求阶段是否考虑安全需求&#xff1f;信息系统安全需求是否经过论证&#xff1f;信息系统安全需求过程是否有业务部门参…

CTFshow—爆破

Web21 直接访问页面的话会弹窗需要输入密码验证&#xff0c;抓个包看看&#xff0c;发现是Authorization认证&#xff0c;Authorization请求头用于验证是否有从服务器访问所需数据的权限。 把Authorization后面的数据进行base64解码&#xff0c;就是我们刚刚输入的账号密码。 …

lin.security提权靶场渗透

声明&#xff01; 学习视频来自B站up主 **泷羽sec** 有兴趣的师傅可以关注一下&#xff0c;如涉及侵权马上删除文章&#xff0c;笔记只是方便各位师傅的学习和探讨&#xff0c;文章所提到的网站以及内容&#xff0c;只做学习交流&#xff0c;其他均与本人以及泷羽sec团队无关&a…

【魅力golang】之-泛型

早期的golang版本是不支持泛型的&#xff0c;这对于从其它语言转型做go开发的程序员来说&#xff0c;非常不友好&#xff0c;自 1.18开始golang正式支持泛型&#xff0c;解决了开发者在编写通用代码时的需求。泛型通过类型参数允许函数和数据结构支持多种类型&#xff0c;从而提…

输入输出流 - cin, cout 的使用

引言 在编程中&#xff0c;输入和输出是与用户交互的重要方式。C 提供了标准输入输出流 cin 和 cout 来处理这些操作。本文将详细介绍如何使用 cin 和 cout 进行基本的输入和输出操作&#xff0c;并通过实例帮助读者更好地理解和掌握这些概念。 一、标准输出流 cout cout 是 …

数据结构(Java)——链表

1.概念及结构 链表是一种 物理存储结构上非连续 存储结构&#xff0c;数据元素的 逻辑顺序 是通过链表中的 引用链接 次序实现的 。 2.分类 链表的结构非常多样&#xff0c;以下情况组合起来就有 8 种链表结构&#xff1a; &#xff08;1&#xff09;单向或者双向 &#xff08;…

pdf有密码,如何实现pdf转换word?

PDF想要转换成其他格式&#xff0c;但是当我们将文件拖到PDF转换器进行转换的时候发现PDF文件带有密码怎么办&#xff1f;今天分享PDF有密码如何转换成word方法。 方法一、 PDF文件有两种密码&#xff0c;打开密码和限制编辑&#xff0c;如果是因为打开密码&#xff0c;建议使…

C++ 面向对象编程:继承中构造与析构函数顺序、继承中的同名属性访问、继承中的同名函数访问

在继承中&#xff0c;构造链中&#xff0c;先构造的后析构 见以下代码示例&#xff1a; #include<iostream> using namespace std;class animal1 { public:animal1() {cout << "animal1 构造" << endl;}~animal1() {cout << "animal1…

Springboot项目下面使用Vue3 + ElementPlus搭建侧边栏首页

Springboot项目下面、在html 页面 Vue3 ElementPlus 搭建侧边栏首页 1、效果图 2、static 文件下面的项目结构 3、代码实现 <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>首页</title><…

Segment Routing Overview

大家觉得有意义和帮助记得及时关注和点赞!!! Segment Routing (SR) 是近年来网络领域的一项新技术&#xff0c;“segment” 在这里 指代网络隔离技术&#xff0c;例如 MPLS。如果快速回顾网络设计在过去几十年的 发展&#xff0c;我们会发现 SR 也许是正在形成的第三代网络设计…

【Rust自学】7.3. use关键字 Pt.1:use的使用与as关键字

喜欢的话别忘了点赞、收藏加关注哦&#xff0c;对接下来的教程有兴趣的可以关注专栏。谢谢喵&#xff01;(&#xff65;ω&#xff65;) 7.3.1. use的作用 use的作用是将路径导入到当前作用域内。而引入的内容仍然是遵守私有性原则&#xff0c;也就是只有公共的部分引入进来才…