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

篇章六 数据结构——链表(二)

目录

1. LinkedList的模拟实现

1.1 双向链表结构图​编辑

1.2 三个简单方法的实现

1.3 头插法

1.4 尾插法

1.5 中间插入

1.6 删除 key

1.7 删除所有key

1.8 clear

2.LinkedList的使用

2.1 什么是LinkedList

5.2 LinkedList的使用

 1.LinkedList的构造

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

3.LinkedList的遍历33.

5.3 ArrayList 和 LinkedList的区别


1. LinkedList的模拟实现

1.1 双向链表结构图

1.2 三个简单方法的实现

@Overridepublic void display() {ListNode cur = head;while (cur != null) {System.out.print(cur.val + " ");cur = cur.next;}System.out.println();}@Overridepublic int size() {int len = 0;ListNode cur = head;while (cur != null) {len++;cur = cur.next;}return len;}@Overridepublic boolean contains(int key) {ListNode cur = head;while (cur != null) {if (cur.val == key) {return true;}cur = cur.next;}return false;}

此处和单链表一样,不在赘述。

1.3 头插法

    @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;}}

1.4 尾插法

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

1.5 中间插入

@Override
public 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 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;
}

此处直接找到 index 位置即可,不需要找 index - 1,因为有 prev

1.6 删除 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;}cur = cur.next;}}

注意:

1.此处最关键的代码:

        cur.prev.next = cur.next;

        cur.next.prev = cur.prev;

2.但是很显然解决不了头节点和尾节点,需要条件语句单独处理

3.特殊情况:

        只有一个节点的链表,且为key值

        需要加上判断逻辑:

                    if (head != null) {
                        head.prev = null;
                    }

1.7 删除所有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;}}}cur = cur.next;}}

注意:

        很显然将 删除key代码中的 return;删除即可

1.8 clear

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

2.LinkedList的使用

2.1 什么是LinkedList

LinkedList 的官方文档

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

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

【说明】

  1. LinkedList实现了List接口

  2. LinkedList的底层使用了双向链表

  3. LinkedList没有实现RandomAccess接口,因此LinkedList不支持随机访问

  4. LinkedList的任意位置插入和删除元素时效率比较高,时间复杂度为O(1)

  5. LinkedList比较适合任意位置插入的场景

5.2 LinkedList的使用

 1.LinkedList的构造

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

3.LinkedList的遍历33.

 public static void main(String[] args) {LinkedList<Integer> linkedList2 = new LinkedList<>();linkedList2.add(1);linkedList2.addLast(3);linkedList2.addLast(4);linkedList2.addLast(5);System.out.println(linkedList2);System.out.println("=========ListIterator=========");ListIterator<Integer> listIterator = linkedList2.listIterator(linkedList2.size());while (listIterator.hasPrevious()) {System.out.print(listIterator.previous() + " ");}System.out.println();System.out.println("=========ListIterator=========");ListIterator<Integer> listIterator1 = linkedList2.listIterator();while (listIterator1.hasNext()) {System.out.print(listIterator1.next() + " ");}System.out.println();System.out.println("=========Iterator=========");Iterator<Integer> iterator = linkedList2.iterator();while (iterator.hasNext()) {System.out.print(iterator.next() + " ");}System.out.println();System.out.println("=========for each=========");for (Integer x : linkedList2) {System.out.print(x + " ");}System.out.println();System.out.println("=========for=========");for (int i = 0; i < linkedList2.size(); i++) {System.out.print(linkedList2.get(i) + " ");}System.out.println();}

5.3 ArrayList 和 LinkedList的区别

http://www.xdnf.cn/news/9528.html

相关文章:

  • 某标杆房企BI平台2.0升级实践
  • 系统思考:心智模式与业务创新
  • LiveGBS海康、大华、宇视、华为摄像头GB28181国标语音对讲及语音喊话:摄像头设备与服务HTTPS准备
  • 工业总线的“F1赛车“与“越野车“:从控制周期解读EtherCAT与CANopen
  • 镍钯金PCB为什么很难做?
  • 伽罗华域(galois field)的乘法计算(异或法)
  • 前后端传输 Long 类型数据时(时间戳,雪花算法ID),精度丢失的根本原因
  • JavaSE核心知识点04工具
  • WebFuture:后台离开站点提示设置关闭后无效
  • 基于Matlab实现指纹识别系统
  • 一招解决 win10 安装 Abobe PR/AE 打不开或闪退
  • 如何在 Solana 上发币,并创建初始流动性让项目真正“动”起来?
  • 12.Java 对象冷冻术:从用户登录到游戏存档的序列化实战
  • 电子电路:开关电路技术深度解析
  • Ubuntu 24.04 LTS 和 ROS 2 Jazzy 环境中使用 Livox MID360 雷达
  • 2025年软件测试面试八股文(含答案+文档)
  • indel_snp_ssr_primer
  • 简历中项目经历怎么写?
  • 硬件服务器基础
  • C++11:系统类型增强
  • ‌ATR2652S双频GNSS低噪声放大器芯片技术解析
  • unityPc端设置了全屏(Exclusive Fullscreen)但是仍然有白边解决办法
  • 在 Linux 中让 ​​Gunicorn​​ 在后台运行(作为守护进程),有几种常用方法:
  • PHP中实现分布式架构的方法与工具全解析!
  • 【pg学习】-账号管理
  • 深入理解Nginx:详尽配置手册
  • Java复习Day21
  • 立体匹配视差图上色代码
  • OC—UI学习-1
  • GoldenDB管理节点zk部署