LinkedList与链表

目录

1、链表 

1.1 链表的概念及结构  

1.2 链表的实现  

2、LinkedList的模拟实现 

3、LinkedList的使用 

3.1 什么是LinkedList  

3.2 LinkedList的使用  

3.3 LinkedList的遍历 

4、ArrayList和LinkedList的区别 


 

在上一篇文章中,我们介绍了ArrayList与顺序表,ArrayList底层使用数组来存储元素,它是具有缺陷的。

ArrayList的缺陷:由于其底层是一段连续空间,当 ArrayList 任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后 搬移,时间复杂度为 O(n),效率比较低,因此 ArrayList 不适合做任意位置插入和删除比较多的场景。因此:java集合中又引入了LinkedList,即链表结构。

1、链表 

1.1 链表的概念及结构  

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。实际中链表的结构非常多样,以下常见的链表结构: 

 1. 单向或者双向

7999f2dc5ef942339efa5d52d6c06e8f.png

2. 带头或者不带头  

93da6e4a968b489e9dadc0cdec461aee.png

3. 循环或者非循环  

0f193c98f5534c568afd48989b6e8742.png

我们主要掌握以下两种结构: 

  • 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。
  • 无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。

1.2 链表的实现  

这里我们实现的是无头单向非循环链表:

//无头单向非循环链表实现
public class MySingleList implements IList{static class ListNode {public int val;public ListNode next;public ListNode(int val) {this.val = val;}}public ListNode head;public void CreateLinkedList() {ListNode node1 = new ListNode(10);ListNode node2 = new ListNode(20);ListNode node3 = new ListNode(30);ListNode node4 = new ListNode(40);node1.next = node2;node2.next = node3;node3.next = node4;this.head = node1;}@Overridepublic void addFirst(int data) {ListNode node = new ListNode(data);if (this.head == null) {this.head = node;}else {node.next = this.head;this.head = node;}}@Overridepublic void addLast(int data) {ListNode node = new ListNode(data);ListNode cur = head;if (this.head == null) {this.head = node;return;}while (cur.next!=null) {cur = cur.next;}cur.next = node;node.next = null;}@Overridepublic void addIndex(int index, int data) throws Indexillegal{if(index < 0||index > size()){//抛自定义异常throw new Indexillegal("插入下标异常:"+index);}if (index==0) {addFirst(data);}if (index == size()) {addLast(data);}ListNode node = new ListNode(data);ListNode cur = searchPre(index);node.next=cur.next;cur.next=node;}private ListNode searchPre(int index) {ListNode cur = head;int count = 0;while (count != index-1) {cur=cur.next;count++;}return cur;}@Overridepublic boolean contains(int key) {ListNode cur = head;while (cur!=null) {if (cur.val==key) {return true;}cur = cur.next;}return false;}@Overridepublic void remove(int key) {if (this.head == null){return;}if (this.head.val == key) {this.head=head.next;return;}ListNode cur = DelPrv(key);if (cur == null) {System.out.println("要删除的元素不存在");}else {ListNode del = cur.next;cur.next=del.next;}}private ListNode DelPrv(int key) {ListNode cur = head;while (cur.next!=null) {if (cur.next.val==key) {return cur;}cur = cur.next;}return null;}@Overridepublic void removeAllKey(int key) {if (this.head == null) {return;}ListNode prv = head;ListNode cur = head.next;while (cur != null) {if (cur.val == key) {prv.next=cur.next;cur=cur.next;}else {prv = cur;cur = cur.next;}}if (this.head.val == key) {this.head=this.head.next;}}@Overridepublic int size() {ListNode cur = head;int count = 0;while (cur!=null) {count++;cur = cur.next;}return count;}@Overridepublic void clear() {ListNode cur = head;while (cur!=null) {ListNode curNext = cur.next;cur.next = null;cur = curNext;}head = null;}@Overridepublic void display() {ListNode cur = head;while (cur!=null) {System.out.print(cur.val+" ");cur = cur.next;}System.out.println();}//从指定位置打印public void display(ListNode node) {ListNode cur = node;while (cur!=null) {System.out.print(cur.val+" ");cur = cur.next;}System.out.println();}
}

其中自定义异常Indexillegal:

public class Indexillegal extends RuntimeException{public Indexillegal(String msg) {super(msg);}
}

2、LinkedList的模拟实现 

LinkedList底层实现就是无头双向循环链表,所以下面是对无头双向链表的实现:

//无头双向链表的实现
public class MyLinkedList implements IList{static class ListNode{public int val;public ListNode next;public ListNode prev;public ListNode(int val) {this.val = val;}}public ListNode head;public ListNode last;@Overridepublic void addFirst(int data) {ListNode listNode = new ListNode(data);if (head == null) {head = listNode;last = listNode;}else {listNode.next = head;head.prev=listNode;head=listNode;}}@Overridepublic void addLast(int data) {ListNode listNode = new ListNode(data);if (head == null){head = listNode;last = listNode;}else {last.next=listNode;listNode.prev=last;last = listNode;}}@Overridepublic void addIndex(int index, int data) {int len = size();if (index < 0|| index > len) {System.out.println("插入异常");return;}if (index==0) {addFirst(data);return;}if (index==len) {addLast(data);return;}ListNode node = new ListNode(data);ListNode cur = FindIndex(index);node.next = cur;cur.prev.next = node;node.prev = cur.prev;cur.prev = node;}private ListNode FindIndex(int index) {ListNode cur = head;while (index != 0){cur = cur.next;index--;}return cur;}@Overridepublic boolean contains(int key) {ListNode cur = head;while (cur!=null) {if (cur.val==key) {return true;}cur = cur.next;}return false;}@Overridepublic void remove(int key) {ListNode cur = head;while (cur != null) {if (cur.val == key) {if (cur == head) {head = head.next;if (head == null) {last = null;}else {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;}}}@Overridepublic void removeAllKey(int key) {ListNode cur = head;while (cur != null) {if (cur.val == key) {if (cur == head) {head = head.next;if (head == null) {last = null;}else {head.prev = null;}}else {cur.prev.next = cur.next;if (cur.next == null) {last = last.prev;}else {cur.next.prev = cur.prev;}}}cur = cur.next;}}@Overridepublic int size() {ListNode cur = head;int count = 0;while (cur!=null) {count++;cur = cur.next;}return count;}@Overridepublic void clear() {ListNode cur = head;while (cur != null) {ListNode curNext = cur.next;cur.next = null;cur.prev = null;cur = curNext;}head = null;last = null;}@Overridepublic void display() {ListNode cur = head;while (cur!=null) {System.out.print(cur.val+" ");cur = cur.next;}System.out.println();}
}

3、LinkedList的使用 

3.1 什么是LinkedList  

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

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

d6c9b91afc7648e79593120cb08c7cfc.png

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

3.2 LinkedList的使用  

1. LinkedList的构造  

方法解释
LinkedList()无参构造
public LinkedList(Collection<? extends E> c)使用其他集合容器中元素构造List 

代码示例:

public static void main(String[] args) {
// 构造一个空的LinkedList
List<Integer> list1 = new LinkedList<>();
List<String> list2 = new java.util.ArrayList<>();
list2.add("JavaSE");
list2.add("JavaWeb");
list2.add("JavaEE");
// 使用ArrayList构造LinkedList
List<String> list3 = new LinkedList<>(list2);
}

2. LinkedList的其他常用方法介绍 

方法解释
boolean add(E e)尾插 e
void add(int index, E element)将 e 插入到 index 位置
boolean addAll(Collection<? extends E> c)尾插 c 中的元素
E remove(int index)删除 index 位置元素
boolean remove(Object o)删除遇到的第一个 o
E get(int index)获取下标 index 位置元素
E set(int index, E element)将下标 index 位置元素设置为 element
void clear()清空
boolean contains(Object o)判断 o 是否在线性表中
int indexOf(Object o)返回第一个 o 所在下标
int lastIndexOf(Object o)返回最后一个 o 的下标
List<E> subList(int fromIndex, int toIndex)
截取部分 list

3.3 LinkedList的遍历 

遍历方法有:foreach遍历和使用迭代器遍历 

代码示例: 

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

4、ArrayListLinkedList的区别 

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

总结:以上就是对LinkedList和链表的总结了,注意它与ArrayList的区别,希望能帮到你们! 

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

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

相关文章

【C++进阶(九)】C++多态深度剖析

&#x1f493;博主CSDN主页:杭电码农-NEO&#x1f493;   ⏩专栏分类:C从入门到精通⏪   &#x1f69a;代码仓库:NEO的学习日记&#x1f69a;   &#x1f339;关注我&#x1faf5;带你学习C   &#x1f51d;&#x1f51d; 多态 1. 前言2. 多态的概念以及定义3. 多态的实…

spring6-国际化:i18n | 数据校验:Validation

文章目录 1、国际化&#xff1a;i18n1.1、i18n概述1.2、Java国际化1.3、Spring6国际化1.3.1、MessageSource接口1.3.2、使用Spring6国际化 2、数据校验&#xff1a;Validation2.1、Spring Validation概述2.2、实验一&#xff1a;通过Validator接口实现2.3、实验二&#xff1a;B…

【刷题-牛客】出栈、入栈的顺序匹配 (代码+动态演示)

【刷题-牛客】出栈、入栈的顺序匹配 (代码动态演示) 文章目录 【刷题-牛客】出栈、入栈的顺序匹配 (代码动态演示) 解题思路 动图演示完整代码多组测试 &#x1f497;题目描述 &#x1f497;: 输入两个整数序列&#xff0c;第一个序列表示栈的压入顺序&#xff0c;请判断第二个…

【机械臂视觉抓取从理论到实战】

首先声明一下,此项目是参考B站哈萨克斯坦UP的【机械臂视觉抓取从理论到实战】 此内容为他研究生生涯的阶段性成果展示和技术分享,所有数据和代码均开源。所以鹏鹏我特此来复现一下,我采用的硬件与之有所不同,UP主使用UR5,我实验室采用的是UR3,下面列出相关材料 UR3CB3.12…

UnitTest框架的使用

文章目录 一、UnitTest框架是什么&#xff1f;二、UnitTest核心要素三、TestCase四、TestSuite & TestRunner 一、UnitTest框架是什么&#xff1f; UnitTest框架是python自带的一个单元测试框架&#xff0c;主要用它来做单元测试&#xff0c;它有以下特点&#xff1a; 能…

Inbound marketing | LTD入站营销是对Hubspot集客营销的升级

你如何理解Inbound marketing&#xff1f; 你如何理解Inbound marketing。 集客营销抑或是入站营销。 2006年&#xff0c;MIT的在校学生BrianHalligan和DharmeshShah&#xff08;hubspot的创始人&#xff09;首次提出Inbound marketing&#xff0c;有别于推广式营销的一种全…

React之render

一、原理 首先&#xff0c;render函数在react中有两种形式&#xff1a; 在类组件中&#xff0c;指的是render方法&#xff1a; class Foo extends React.Component {render() {return <h1> Foo </h1>;} }在函数组件中&#xff0c;指的是函数组件本身&#xff1a…

【Mysql】Mysql中的B+树索引(六)

概述 从上一章节我们了解到InnoDB 的数据页都是由7个部分组成&#xff0c;然后各个数据页之间可以组成一个双向链表 &#xff0c;而每个数据页中的记录会按照主键值从小到大的顺序组成一个单向链表 &#xff0c;每个数据页都会为存储在它里边儿的记录生成一个页目录 &#xff…

如何查看SSL证书是OV还是DV?

网站的安全性与信任度对于用户来说至关重要&#xff0c;它决定着用户是否继续浏览以及是否与您开展业务。SSL证书则是确保网站能够通过HTTPS加密安全传输数据的基础&#xff0c;可确保网站的安全可信。部署了SSL证书的网站打开后&#xff0c;在浏览器地址栏处会有安全锁标志。而…

台灯显色指数多少好?推荐显色指数优秀的护眼台灯

台灯的显色指数是其非常重要的指标&#xff0c;它可以表示灯光照射到物体身上&#xff0c;物体颜色的真实程度&#xff0c;一般用平均显色指数Ra来表示&#xff0c;Ra值越高&#xff0c;灯光显色能力越强。常见的台灯显色指数最低要求一般是在Ra80以上即可&#xff0c;比较好的…

Pyecharts绘图教程(2)—— 绘制多种折线图(Line)参数说明+代码实战

文章目录 &#x1f3af; 1 简介&#x1f3af; 2 图表配置项2.1 导入模块2.2 数据配置项2.3 全局配置项 &#x1f3af; 3 代码实战3.1 基础折线3.2 平滑曲线&#xff08;is_smooth&#xff09;3.3 阶梯折线&#xff08;is_step&#xff09;3.4 空值过渡&#xff08;is_connect_n…

Hadoop3教程(三十三):(生产调优篇)慢磁盘监控与小文件归档

文章目录 &#xff08;161&#xff09;慢磁盘监控&#xff08;162&#xff09;小文件归档小文件过多的问题如何对小文件进行归档 参考文献 &#xff08;161&#xff09;慢磁盘监控 慢磁盘&#xff0c;是指写入数据时特别慢的一类磁盘。这种磁盘并不少见&#xff0c;当机器运行…