Java:数据结构-LinkedList与链表(1)

news2024/10/11 21:55:25

一 链表

1.. ArrayList的缺陷(LinkedList的优点)

在ArrayList任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后 搬移,时间复杂度为O(n),效率比较低,因此ArrayList不适合做任意位置插入和删除比较多的场景。

2.什么是链表

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。我们可以把链表想象成火车的车厢连接在一起。

3.链表的八种形式

最常用的为单向链表

还有双向链表,带头或者不带头链表,循环或者非循环链表 

 4.链表的实现

public interface IList {
        //头插法
        public void addFirst(int data);
        //尾插法
        public void addLast(int data);

        //任意位置插入,第一个数据节点为0号下标
        public void addIndex(int index,int data);

        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key);

        //删除第一次出现关键字为key的节点
        public void remove(int key);

        //删除所有值为key的节点
        public void removeAllKey(int key);

        //得到单链表的长度
         public int size();

         public void clear();

         public void display();

}

1.头插法

public class MyArrayList implements IList {

    static class ListCode {
        public int val;
        public ListCode next;


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

    public ListCode head;

public void addFirst(int data) {
        ListCode node=new ListCode(data);
        node.next=head;
        head=node;
    }
}

2.尾插法

public class MyArrayList implements IList {

    static class ListCode {
        public int val;
        public ListCode next;


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

    public ListCode head;

public void addLast(int data) {
        ListCode cur=head;
        ListCode node=new ListCode(data);
        if(cur==null){
            head=node;
            return;
        }
        while (cur!=null){
            cur=cur.next;
        }
        cur.next=node;

    }
}

3.得到单链表的长度

public class MyArrayList implements IList {

    static class ListCode {
        public int val;
        public ListCode next;


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

    public ListCode head;

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

4.任意位置插入,第一个数据节点为0号下标

 

public class IllegalException extends RuntimeException{
    public IllegalException(){
    }
    public IllegalException(String mas){
        super(mas);
    }
}
public class MyArrayList implements IList {

    static class ListCode {
        public int val;
        public ListCode next;


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

    public ListCode head;
 public void addIndex(int index, int data) throws IllegalException{
       try {
           int len=size();
           if(index<0 || index>len){
               throw new IllegalException("pos位置不合法");
           }
           if(len==0){
               addFirst(data);
           }
           if (index==len-1){
               addLast(data);
           }
           ListCode node=new ListCode(data);
           ListCode cur=head;
           while (index!=0){
               cur=cur.next;
               index--;
           }
           node.next=cur.next;
           cur.next=node;

       }catch (IllegalException e){
           e.printStackTrace();
           System.out.println("pos位置不合法");
       }


    }
}

5.查找是否包含关键字key是否在单链表当中

 

public class MyArrayList implements IList {

    static class ListCode {
        public int val;
        public ListCode next;


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

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

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

public class MyArrayList implements IList {

    static class ListCode {
        public int val;
        public ListCode next;


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

    public ListCode head;
    public void remove(int key) {
       if (head==null){
           return;
       }
       if(head.val==key){
           return;
       }
       ListCode cur=findNodeOfKey(key);
       if(cur==null){
           return;
       }
       ListCode del=cur.next;
       cur.next=del.next;

    }
    public ListCode findNodeOfKey(int key){
        ListCode cur=head;
        while (cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }
}

7. 删除所有值为key的节点

 

public class MyArrayList implements IList {

    static class ListCode {
        public int val;
        public ListCode next;


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

    public ListCode head;

public void removeAllKey(int key) {
        if(head==null){
            return;
        }
        ListCode cur=head.next;
        ListCode pre=head;
        while (cur!=null){
            if(cur.val==key){
                pre.next=cur.next;
                cur=cur.next;
            }else {
                pre=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
        }
    }
}

8.清空单链表

 

public class MyArrayList implements IList {

    static class ListCode {
        public int val;
        public ListCode next;


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

    public ListCode head;

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

 希望能对大家有所帮助!!!!

 

 

 

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

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

相关文章

【AI知识点】残差网络(ResNet,Residual Networks)

AI知识点总结&#xff1a;【AI知识点】 AI论文精读、项目、思考&#xff1a;【AI修炼之路】 残差网络&#xff08;ResNet&#xff0c;Residual Networks&#xff09; 是由微软研究院的何凯明等人在 2015 年提出的一种深度神经网络架构&#xff0c;在深度学习领域取得了巨大的成…

Vue3封装消息提示框-基于element-plus

Vue3封装消息提示框-基于element-plus 图片示例 封装代码 创建modal.js文件 import {ElMessage,ElMessageBox,ElNotification,ElLoading, } from "element-plus";let loadingInstance;export default {// 消息提示msg(content) {ElMessage.info(content);},// 错误…

手机移动终端的土壤检测

手机OTG转USB串口&#xff0c;读取土壤检测设备信息&#xff0c;在APP展示。 总结一下 1. 用了MAUI框架&#xff0c;这东西感觉比xamarin好用&#xff0c;特别是contentpage和单例模式&#xff0c;数据绑定也很OK。 2. 串口驱动不好孤岛&#xff0c;废了不少功夫专门做这个。 3…

Lory: 推进大型语言模型训练的新篇章

人工智能咨询培训老师叶梓 转载标明出处 随着模型规模的增长&#xff0c;如何有效训练并利用这些模型成为了一个挑战。陈丹琦团队一项新的研究提出了一种创新的预训练方法——Lory&#xff0c;旨在解决大模型在混合专家&#xff08;MoE&#xff09;架构中的可微分性和计算效率…

开关打开输入框才能输入文字,否则为禁用状态

页面开关默认为关闭状态&#xff0c;输入框为禁用状态。 当点击开关&#xff0c;打开开关后&#xff0c;输入框禁用状态解除&#xff0c;才可以在输入框内输入。 html结构: <div class"page_top"><!-- 第一行 --><div class"top_first">…

使用three.js 实现一个 马赛克得 shader

使用three.js 实现一个 马赛克得 shader 源链接&#xff1a;https://threehub.cn/#/codeMirror?navigationThreeJS&classifyshader&idmosaicShader 国内站点预览&#xff1a;http://threehub.cn github地址: https://github.com/z2586300277/three-cesium-example…

HTML的介绍

HTML HTML是一种超文本标记语言,超文本是指,除了文本之外,还可能包含图片,音频,或者评注等的 文本形式,比文本强大,通过链接和交互方式来组织和呈现信息.标记语言是指,由标签构成的语言.HTML定义了多种不同的标签,用来表示不同的内容. 标签的介绍: 1.<h3> 三级 </h3&…

增强AI查询:使用Rewrite Retrieve Read框架优化RAG

增强AI查询&#xff1a;使用Rewrite Retrieve Read框架优化RAG 引言 在大规模语言模型&#xff08;LLM&#xff09;中&#xff0c;通过查询重写来提升检索增强生成&#xff08;RAG&#xff09;的性能是一个热门研究领域。本文将介绍如何使用rewrite_retrieve_read模板来优化R…

基于SpringBoot的图书推荐系统的设计与实现(论文+源码)_kaic

摘 要 网络信息技术的高速发展&#xff0c;使得高校图书馆的服务空间日益扩大&#xff0c;依据个人特点的针对性服务逐渐成为新服务模式的主导趋势。对于大多数用户而言&#xff0c;很难在大量的学术图书馆中快速找到他们想要的材料。另外&#xff0c;随着时代的不断发展&am…

Mysql的LSN是什么?

LSN的含义 ​ LSN全称为 Log Sequence Number&#xff0c;即日志序列号。它是一个不断递增的数字&#xff0c;用来标识事务日志中的每个操作或事件。LSN是一个64位的数字&#xff0c;每一个LSN值都是唯一的&#xff0c;并且随时间线性增加。 ​ 通过SHOW ENGINE INNODB STATUS;…

GADBench Revisiting and Benchmarking Supervised Graph Anomaly Detection

Neurips 23 推荐指数&#xff1a; #paper/⭐⭐⭐ 领域&#xff1a;图异常检测 胡言乱语&#xff1a; neurips 的benchmark模块的文章总能给人一些启发性的理解&#xff0c;这篇的insight真有意思。个人感兴趣的地方会加粗。此外&#xff0c;这篇文章和腾讯AIlab合作&#xff…

嵌入式基本知识

文章目录 调试接口仿真器MCU实际的调试接口 调试接口 调试接口用于对MCU进行编程和调试&#xff0c;这里的编程指将源代码编译后的.hex文件写入MCU闪存特定地址中&#xff0c;调试指MCU运行代码debug的过程。 不同的接口协议有不同的接口类型。SWD协议调试接口的引脚主要有&a…

卡码网C++基础课 |20. 排队取奶茶

目录 前言 一、题目描述 二、解题思路 1.队列 2.队列的操作 三、完整代码 总结 前言 仅个人记录所用 源自卡码网的C基础课 “这门C基础课 帮助 编程零基础学员快速学习刷算法题所需要的基础语法知识&#xff0c;学完之后&#xff0c;再来刷代码随想录&#xff0c;或者自己去…

CentOS 7.9 局域网配置指定同步时间服务器

在 CentOS 7.9 中&#xff0c;默认的时间同步工具是 chrony。以下是如何配置 NTP 服务器地址并使用 chrony 进行时间同步的步骤&#xff1a; 1. 安装 chrony&#xff08;通常已经预装可忽略&#xff09; 通过systemctl status chronyd检查是否已经安装启动 如果没网可以直接…

npm安装依赖报错npm ERR! Unexpected token ‘.

电脑是windows的&#xff0c;因为有多个项目做开发&#xff0c;每个项目需要的node版本不一样&#xff0c;所以使用了nvm做node管理。 电脑的nvm是1.1.7版本的。 新项目在安装依赖时突然报错如下&#xff1a; npm ERR! Unexpected token .在网上查了很多都说是nvm版本太低了&…

【MLP-Mixer】核心方法解读

abstract&#xff1a; 我们提出MLP-Mixer架构(或简称“Mixer”)&#xff0c;这是一个具有竞争力但在概念和技术上都很简单的替代方案&#xff0c;它不使用卷积或自关注。相反&#xff0c;Mixer的架构完全基于多层感知器(mlp)&#xff0c;这些感知器可以在空间位置或特征通道上…

渗透测试 之 域渗透手法【域内用户枚举】手法 Kerbrute msf pyKerbrute 工具使用详解

说明一下: 域内用户枚举工具使用说说&#xff1a; Kerbrute pyKerbrute MSF模块的使用 域内用户名枚举原理分析&#xff1a; 域内用户枚举攻击防御&#xff1a; 流量检测&#xff1a; 日志层面&#xff1a; 说明一下: 域环境或者内网环境下&#xff0c;可以在没有域环…

深入理解Transformer的笔记记录(精简版本)---- ELMO->GPT->BERT

1、ELMO word embedding无法区分多义词的不同语义,其本质上是个静态的方式,所谓静态指的是训练好之后每个单词的表达就固定住了,以后使用的时候,不论新句子上下文单词是什么,这个单词的Word Embedding不会跟着上下文场景的变化而改变 ELMO根据当前上下文对Word Embed…

有趣的python库:用 difflib 实现文本差异的可视化

一&#xff0c;介绍 difflib 模块是Python标准库的一部分&#xff0c;提供了一系列用于比较序列的类和函数&#xff0c;特别适用于文本比较任务。这个模块可以帮助用户发现两个文本文件或字符串序列之间的差异&#xff0c;并以多种格式展示这些差异&#xff0c;比如这样&#…

400行程序写一个实时操作系统RTOS(开篇)

笔者之前突发奇想&#xff0c;准备写一个极其微小的实时操作系统内核&#xff0c;在经过数天的努力后&#xff0c;这个RTOS诞生了。令读者比较意外的是&#xff0c;它的程序只有400行左右。但就是这短短的400行&#xff0c;完成了动态内存管理、多线程、优先级、低功耗管理、调…