当前位置: 首页 > news >正文

【Java数据结构】---List(LinkedList)

乐观学习,乐观生活,才能不断前进啊!!!

我的主页:optimistic_chen

我的专栏:c语言 ,Java

欢迎大家访问~
创作不易,大佬们点赞鼓励下吧~

文章目录

  • 前言
  • 链表(MySingleList)
    • 具体功能代码
  • LinkedList简介
  • LinkedList的模拟实现
  • LinkedList的使用
    • LinkedList的构造
    • LinkedList的方法
    • LinkedList的遍历
  • ArrayList和LinkeddList的区别
  • 完结

前言

在这里插入图片描述
上篇博客详细写了ArrayList的相关问题,包括上图(极其重要),我会在最近几篇博客中都有附上。

ArrayList的优点很明显,底层逻辑是一个数组,它通过下标去访问数据的速度非常快。但是在ArrayList任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为O(n),效率比较低

所以java集合框架中引入了LinkedList类,即链表结构。

链表(MySingleList)

链表,作为线性表的一种,它与顺序表截然相反:链表是一种物理存储结构 非连续存储结构, 数据元素的逻辑顺序是通过链表中的引用 链接次序实现的

之前C语言了解过这方面的知识C语言—链表专题,所以我们有一定的基础,我相信链表对于大家一定是熟悉的存在。我们先由易入难,先自己尝试一下单链表(MySingleList)的功能实现,为接下来的 LinkedList(无头双向链表) 打下基础.

接口

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

具体功能代码

头插法

@Overridepublic void addFirst(int data) {ListNode node = new ListNode(data);node.next=head;head=node;}

尾插法

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

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

@Overridepublic void addIndex(int index, int data) {int len=size();if(index<0||index>len){System.out.println("index位置不合法");return;}if(index==0){addFirst(data);return;}if(index==len){addLast(data);return;}ListNode cur=head;while(index-1!=0){cur=cur.next;index--;}ListNode node=new ListNode(data);node.next= cur.next;cur.next=node;}

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

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

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

@Overridepublic void remove(int key) {if(head==null){return;}if(head.val==key){head=head.next;return;}ListNode cur=findNodeOfKey(key);if(cur==null){return;}ListNode del= cur.next;cur.next=del.next;}private ListNode findNodeOfKey(int key){ListNode cur=head;while(cur.next!=null) {if (cur.next.val == key) {return cur;}cur= cur.next;}return null;}

删除所有值为key的节点

@Overridepublic void removeAllKey(int key) {if(head==null){return;}ListNode prev=head;ListNode cur=head.next;while(cur!=null){if(cur.val==key){prev.next=cur.next;cur= cur.next;}else{prev=cur;cur= cur.next;}if(head.val==key){head=head.next;return;}}}

得到单链表的长度

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

清空链表

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

LinkedList简介

在这里插入图片描述

在这里插入图片描述

LinkedList的底层是双向链表结构由于链表没有将元素存储在连续的空间中,元素存储在单独的节点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高。

总结:

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

LinkedList的模拟实现

在这里插入图片描述

头插法

@Overridepublic void addFirst(int data) {ListNode node=new ListNode(data);if(head==null){head=last=node;}else{node.next=head;head.prev=node;head=node;}}

尾插法

@Overridepublic void addLast(int data) {ListNode node=new ListNode(data);if(head==null){head=last=node;}else{last.next=node;node.prev=last;last= last.next;}}

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

@Overridepublic void addIndex(int index, int data) {int len=size();if(index<0||index>len){return;}if(index==0){addFirst(data);return;}if(index==len){addLast(data);return;}ListNode cur=findIndex(index);ListNode node=new ListNode(data);node.next=cur;cur.prev.next=node;node.prev=cur.prev;cur.next=node;}
private ListNode findIndex(int index){ListNode cur=head;while(index!=0){cur= cur.next;index--;}return cur;}

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

@Overridepublic boolean contains(int key) {ListNode cur=head;int count=0;while(cur!=null){if(cur.val==key) {return true;}cur= cur.next;}return false;}

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

@Overridepublic void remove(int key) {ListNode cur=head;while(cur!=null){//开始删除if(cur.val==key){//有可能是头节点if(cur==head){head = head.next;if(head!=null){head.prev=null;}}else{cur.prev.next= cur.next;if(cur.next==null){//有可能是尾节点last= last.prev;}else{cur.next.prev= cur.prev;}}return;}else{cur = cur.next;}}}

删除所有值为key的节点

@Overridepublic void removeAllKey(int key) {ListNode cur=head;while(cur!=null){//开始删除if(cur.val==key){//有可能是头节点if(cur==head){head = head.next;if(head!=null){head.prev=null;}}else{cur.prev.next= cur.next;if(cur.next==null){//有可能是尾节点last= last.prev;}else{cur.next.prev= cur.prev;}}}else{cur = cur.next;}}}

LinkedList的使用

前面我们自主实现了LinkedList的各种方法,只是为了大家更好的理解和学习,以后做题可能会运用到类似的想法或思路,可以更加高效的完成题目,平时也可以直接使用LinkedList下的方法。如下图:
在这里插入图片描述

LinkedList的构造

在这里插入图片描述

public static void main(String[] args) {List<Integer> list1=new LinkedList<Integer>();LinkedList<Integer> list2 = new LinkedList<Integer>();//使用ArrayList构造LinkedList     List<String> list3=new ArrayList<>();List<String> list4=new LinkedList<>(list3);}

为什么可以有的构造可以用List,有的用LinkedList,甚至出现了ArrayList?一切都是这张贯穿整个数据结构的图。
在这里插入图片描述

LinkedList的方法

public static void main(String[] args) {LinkedList<Integer> list = new LinkedList<>();list.add(1);  // add(): 表示尾插list.add(2);list.add(3);list.add(4);list.add(5);list.add(6);list.add(7);System.out.println(list.size());System.out.println(list);// subList(from, to): 用list中[from, to)之间的元素构造一个新的LinkedList返回List<Integer> list2 = list.subList(0, 3); System.out.println(list);System.out.println(list2);list.clear();        // 将list中元素清空System.out.println(list.size());
}

LinkedList的遍历

之前ArrayList博客【Java数据结构】—List(ArrayList) 讲过线性表的遍历,这次补充一下…

public static void main(String[] args) {LinkedList<Integer> list = new LinkedList<>();list.add(1);  // add(): 表示尾插list.add(2);list.add(3);list.add(4);list.add(5);list.add(6);list.add(7);System.out.println(list.size());// 使用反向迭代器---反向遍历ListIterator<Integer> it = list.listIterator(list.size());while (it.hasPrevious()){System.out.print(it.previous() +" ");}
}

ArrayList和LinkeddList的区别

不同ArrayListLinkedList
存储空间物理上一定连续逻辑上连续,但物理上不一定连续
随机访问支持O(1)不支持O(n)
头插空间不够时扩容不存在容量的概念
运用场景元素高效存储+频繁访问频繁任意位置的插入和删除

完结

好了,到这里Java语法部分就已经结束了~
如果这个系列博客对你有帮助的话,可以点一个免费的赞并收藏起来哟~
可以点点关注,避免找不到我~ ,我的主页:optimistic_chen
我们下期不见不散~~Java

下期预告: 【Java数据结构】- - - List

http://www.lryc.cn/news/425156.html

相关文章:

  • 开发军用LabVIEW程序注意事项
  • A3VLM: Actionable Articulation-Aware Vision Language Model
  • 企业高性能web服务器
  • 数据库:深入解析SQL分组与聚合——提升数据查询效率的关键技巧
  • 【CSS】数字英文css没有转换成...换行点、没有换行、拆分的问题(非常常见的需求)
  • C++ string模拟实现
  • Lora 全文翻译
  • 结题阶段(2024年8月)
  • 贪吃蛇(C语言详解)
  • 国际以太网专线(IEPL)与国际专线(IPLC)服务
  • vue 子父组件互相改值
  • java之拼图小游戏(开源)
  • Linux Shell批量测试IP连通性
  • 已解决:anaocnda如何备份环境与安装环境
  • 自动化与高效设计:推理技术在FPGA中的应用
  • 对react模块和模块化理解
  • CAN总线-----帧格式
  • UE网络同步(一) —— 一个项目入门UE网络同步之概念解释
  • MATLAB中rsf2csf函数用法
  • Java基础 文字小游戏
  • 「数组」归并排序 / if语句优化|小区间插入优化(C++)
  • 颠覆传统 北大新型MoM架构挑战Transformer模型,显著提升计算效率
  • 接口优化笔记
  • pandas 科学计数法显示
  • PHP正则替换字符串中的图片地址
  • 基于多商户AI智能名片商城小程序的粉丝忠诚度提升策略:深度融合足额法则与多维度激励体系
  • BigDecimal高精度运算
  • C/C++实现蓝屏2.0
  • Unity音频管理器插件AudioToolKit
  • 搜维尔科技:驾驶模拟器背后的技术: Varjo的虚拟/混合现实 (VR/XR)提供独特的优势,最终加快汽车开发创新的步伐