Java——LinkedList

news2024/9/22 7:15:04

1、链表

1.1 链表的概念及结构

链表在逻辑层面上是连续的,在物理层面上不一定是连续的

链表结构可分为,单向或双向、带头或不带头、循环或非循环,组合共计8种

重点:无头单向非循环链表、无头双向链表

1.2 模拟实现无头单向非循环链表

一个链表由若干节点组成,结合 内部类 知识,可将节点类定义在链表类中,成为内部类

public class MySingleLinkedList {

    static class ListNode {//该内部类中定义的是节点的属性
        public int val;
        public ListNode next;

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

    public ListNode head;//链表的头节点,定义在MySingleLinkedList类中
    
}

下面以穷举的方式创建链表方便理解(真正创建链表并非如此)

//MySingleLinkedList类

    //该方法创建一个链表,头节点为node1,当该方法结束后,变量node1...都会销毁,只剩下头节点为head的链表
    public void createdList(){
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }

打印链表

//MySingleLinkedList 类

    public void display() {
        ListNode cur = head; //若直接使用head进行遍历打印,将只能打印一次,再次调用该函数,头节点就不在原来的位置了
        while(cur != null) { //注意!此处若写成 cur.next != null 则不会打印最后一个节点
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }

求链表长度:

    //遍历即可
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

头插法:

    public void addFirst(int data) {
        ListNode newNode = new ListNode(data);
        newNode.next = head;
        head = newNode;
    }

尾插法:

    public void addLast(int data) {
        ListNode newNode = new ListNode(data);
        //不要忘记:判空!
        if(head == null) {
            head = newNode;
            return;
        }
        ListNode cur = head;
        while(cur.next != null) { //此处若写成 cur.next != null 则不会打印最后一个节点
            cur = cur.next;
        }
        cur.next = newNode;
    }

在index位置插入

// IndexNotLegalException 异常类
public class IndexNotLegalException extends RuntimeException{
    public IndexNotLegalException() {

    }
    public IndexNotLegalException(String msg) {
        super(msg);
    }
}


// MySingleLinkedList 类
    public void addIndex(int index, int data) {
        //1、判断index合法性
        try{
            checkIndex(index);
        }catch(IndexNotLegalException e) {
            e.printStackTrace();
        }
        //2、index == 0 || index == size()
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        //3、找到index的前一个位置
        ListNode cur = findIndexSubOne(index);
        //4、进行连接
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }
    private void checkIndex(int index) throws IndexNotLegalException{
        if(index < 0 || index > size()) {
            throw new IndexNotLegalException("index不合法!");
        }
    }
    private ListNode findIndexSubOne(int index) {
        int count = 0;
        ListNode cur = head;
        while(count < index-1) {
            cur = cur.next;
            count++;
        }
        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) {
        //判空
        if(head == null) {
            return;
        }
        //若头节点为key
        while(head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = head;
        //遍历找到值为key的节点
        while(cur.next != null) {
            if(cur.next.val == key) {
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
        System.out.println("没有找到要删除的数字!");
    }

删除所有值为key的节点

    public void removeAllKey(int key) {
        //判空
        if (this.head == null) {
            return;
        }
        //快慢指针
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
            }else {
                prev = cur;
            }
            cur = cur.next;
        }
        //处理头节点,当上述操作完成后,只剩下头节点没有判断
        //该方法优于一上来就判断头节点
        if(head.val == key) {
            head =head.next;
        }
    }

清空链表

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

1.3 模拟实现无头双向非循环链表

public class MyLinkedList {
    static class ListNode {
        public int data;
        public ListNode prev;//前驱节点
        public ListNode next;//后继节点

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

    public ListNode head;//头节点
    public ListNode last;//尾节点

    //得到单链表的长度
    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.data + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        while(cur != null) {
            if(cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            head = last = node;
        }else {
            head.prev = node;
            node.next = head;
            head = node;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(head == null) {
            head = last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        try{
            checkIndex(index);
        }catch(IndexIllegalException e) {
            e.printStackTrace();
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
    }
    private ListNode findIndex(int index) {
        ListNode cur = head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    private void checkIndex(int index) throws IndexIllegalException{
        if(index < 0 || index > size()) {
            throw new IndexIllegalException("双向链表index不合法!");
        }
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        while(cur != null) {
            if(cur.data == key) {
                if(cur == head) {
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else if(cur == last) {
                    cur.prev.next = null;
                    last = cur.prev;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;

            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = head;
        while(cur != null) {
            if(cur.data == key) {
                if(cur == head) {
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else if(cur == last) {
                    cur.prev.next = null;
                    last = cur.prev;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }

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

链表遍历方式:

    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>();
        list.add(1);
        list.add(2);
        list.add(3);

        //直接sout打印
        System.out.println(list);
        System.out.println("======");

        //foreatch循环打印
        for(Integer x : list) {
            System.out.print(x + " ");
        }
        System.out.println();
        System.out.println("======");

        //for循环打印
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
        System.out.println();
        System.out.println("======");

        //Iterator打印
        Iterator<Integer> it = list.iterator();
        while(it.hasNext()) {
            System.out.print(it.next() + " ");
        }
        System.out.println();
        System.out.println("======");

        //ListIterator可以倒着打印
        ListIterator<Integer> it3 = list.listIterator(list.size());
        while(it3.hasPrevious()) {
            System.out.print(it3.previous() + " ");
        }
    }

运行结果:

2、ArrayList 和 LinkedList 的区别

不同点ArrayListLinkedList
存储空间上逻辑&物理均连续逻辑上连续,物理上不一定连续
随机访问支持,时间复杂度为O(1)不支持,时间复杂度为O(N)
头插需要搬运元素,O(N)只需修改引用的指向,O(1)
插入空间不够时需要扩容没有容量概念
应用场景元素高效存储+频繁访问频繁在任意位置插入删除操作

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

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

相关文章

某信用合作社数据架构规划方案(115页PPT)

方案介绍&#xff1a;为应对数字化转型挑战&#xff0c;某信用合作社计划实施一套新的数据架构&#xff0c;以提高数据处理效率、确保数据安全&#xff0c;并满足业务快速发展的需求。预期成效是完善的数据架构能够全面地提升我社六个方面的竞争能力&#xff0c;更好地服务于目…

大模型辅助编程助手:『小浣熊 Raccoon』 如何使用?

认识 Raccoon Raccoon (Raccoon is Another Code CO-pilOt Navigator) 是基于 AI 的代码助手&#xff0c;是商汤科技发布基于商汤自研大语言模型的智能编程助手&#xff0c;代码小浣熊 Raccoon 支持 Python、Java、JavaScript、C、Go、SQL 等30主流编程语言和 VS Code、Intell…

OpenGL3.3_C++_Windows(3)

GLSL Shader基础 Shader&#xff08;把输入转化为输出,运行在GPU上&#xff09;&#xff1a;首先要声明版本&#xff0c;有各自的入口点main&#xff08;&#xff09;顶点数据上限:16个包含4分量&#xff1a;16 * 4 64个分量向量&#xff1a;容器vec。使用.x、.y、.z和.w&am…

docker一些常用命令以及镜像构建完后部署到K8s上

docker一些常用命令以及镜像构建完后部署到K8s上 1.创建文件夹2.删除文件3.复制现有文件内容到新建文件4.打开某个文件5.查看文件列表6.解压文件&#xff08;tar格式&#xff09;7.解压镜像8.查看镜像9.删除镜像10.查看容器11.删除容器12.停止运行容器13.构建镜像14.启动容器15…

200元的5G热点机能作为渗透测试测试机,还能当128G移动硬盘,怎么算都值

最近&#xff0c;迫于很多的app渗透测试&#xff0c;急需一个真机&#xff0c;在咸鱼上发现了一款低价5G手机&#xff0c;平时可以当随身WiFi&#xff0c;还可以进行app渗透测试&#xff0c;它就是中兴远航30。 中兴远航30是2022年4月发布的机器&#xff0c;全系只有4G128G和6G…

单例模式、工厂模式 c++关键字 static

static 关键字的作用&#xff1a; 主要作用在于 控制变量或函数的作用域、生命周期以及它们如何被不同部分的程序访问&#xff0c;从而帮助程序员管理内存、避免命名冲突&#xff0c;并实现特定的设计模式&#xff08;如单例模式&#xff09;。 1. 静态局部变量&#xff1a;当…

Unity Protobuf+RPC+UniTask

远程过程调用&#xff08;RPC&#xff09;协议详解 什么是RPC协议RPC的基本原理RPC的关键组件RPC的优缺点Protobuf函数绑定CallEncodeRecvDecodeSocket.Send和Recv项目地址 什么是RPC协议 远程过程调用&#xff08;Remote Procedure Call&#xff0c;简称RPC&#xff09;是一种…

配置Windows客户端连接iSCSI设备

1、运行iSCSI发起程序 控制面板–>系统和安全–>管理工具–>iSCSI发起程序。 2、更改客户端iqn属性 3、点击连接&#xff0c;就会在本次磁盘新加一款硬盘。 4、通过格式化新建卷就可使用该硬盘。

eBay测评,自养号应该如何做?

测评自养号就是自己搭建国外的服务器和IP环境&#xff0c;实现自己注册eBay的买家账号&#xff0c;通过电脑端环境一台电脑就可以无限养号&#xff0c;一次可以开十几个窗口同时浏览下单&#xff0c;每个窗口都是独立的环境&#xff0c;一账号一环境一IP一卡 买家账号掌握在卖…

ARM32开发--存储器介绍

知不足而奋进 望远山而前行 目录 文章目录 前言 存储器分类 RAM ROM EEPROM Flash 总结 前言 在现代计算机系统中&#xff0c;存储器扮演着至关重要的角色&#xff0c;不仅影响着数据的存取速度和稳定性&#xff0c;还直接关系到计算机系统的性能和应用场景的选择。存…

ARM32开发--IIC时钟案例

知不足而奋进 望远山而前行 目录 文章目录 前言 目标 内容 需求 开发流程 移植驱动 修改I2C实现 测试功能 总结 前言 在现代嵌入式系统开发中&#xff0c;移植外设驱动并测试其功能是一项常见的任务。本次学习的目标是掌握移植方法和测试方法&#xff0c;以实现对开…

热门开源大模型项目推荐

一&#xff1a;开源大模型热门项目推荐 NNI&#xff1a;由微软发布的开源AutoML工具包&#xff0c;支持神经网络超参数调整。最新版本对机器学习生命周期的各个环节做了全面支持&#xff0c;包括特征工程、神经网络架构搜索(NAS)、超参调优和模型压缩。适用于各种机器学习项目&…

三极管的厄利效应(early effect)

詹姆斯M厄利(James M. Early)发现的现象&#xff0c;厄利效应&#xff08;英语&#xff1a;Early effect&#xff09;&#xff0c;又译厄尔利效应&#xff0c;也称基区宽度调制效应&#xff0c;是指当双极性晶体管&#xff08;BJT&#xff09;的集电极&#xff0d;射极电压VCE改…

DETR实现目标检测(二)-利用自己训练的模型进行预测

1、图片预测&#xff08;CPU&#xff09; 关于DETR模型训练自己的数据集参考上篇文章&#xff1a; DETR实现目标检测(一)-训练自己的数据集-CSDN博客 训练完成后的模型文件保存位置如下&#xff1a; 准备好要预测的图片&#xff1a; 然后直接调用模型进行预测&#xff0c;并设…

基于springboot实现入校申报审批系统项目【项目源码+论文说明】计算机毕业设计

基于springboot实现入校申报审批系统演示 摘要 传统办法管理信息首先需要花费的时间比较多&#xff0c;其次数据出错率比较高&#xff0c;而且对错误的数据进行更改也比较困难&#xff0c;最后&#xff0c;检索数据费事费力。因此&#xff0c;在计算机上安装入校申报审批系统软…

ChatGPT中文镜像网站分享

ChatGPT 是什么&#xff1f; ChatGPT 是 OpenAI 开发的一款基于生成预训练变换器&#xff08;GPT&#xff09;架构的大型语言模型。主要通过机器学习生成文本&#xff0c;能够执行包括问答、文章撰写、翻译等多种文本生成任务。截至 2023 年初&#xff0c;ChatGPT 的月活跃用户…

CV每日论文--2024.6.14

1、ICE-G: Image Conditional Editing of 3D Gaussian Splats 中文标题&#xff1a;ICE-G&#xff1a;3D 高斯斑点的图像条件编辑 简介&#xff1a;近年来,出现了许多技术来创建高质量的3D资产和场景。然而,当涉及到这些3D对象的编辑时,现有方法要么速度慢、要么牺牲质量,要么…

基于Python+OpenCV高速公路行驶车辆的速度检测系统

简介&#xff1a; 基于Python和OpenCV的高速公路行驶车辆的速度检测系统旨在实时监测高速公路上的车辆&#xff0c;并测量它们的速度。该系统可以用于交通监控、道路安全管理等领域&#xff0c;为相关部门提供重要的数据支持。 系统实现&#xff1a; 视频流输入&#xff1a;系…

Java项目中使用OpenCV检测人脸的应用

Java项目中使用OpenCV检测人脸的应用 一、准备工作 将下载好的opencv的jar包放在项目的根目录下&#xff0c;可以新建一个lib的文件夹&#xff0c;将其放在此处&#xff1b; 在pom文件中引入&#xff1a; <profiles><!-- 生产环境 --><profile><id>…

用智能插件(Fitten Code: Faster and Better AI Assistant)再次修改vue3 <script setup>留言板

<template><div><button class"openForm" click"openForm" v-if"!formVisible">编辑</button><button click"closeForm" v-if"formVisible">取消编辑</button><hr /><formv-i…