5.LinkedList

news/2025/2/13 23:54:29/
LinkedList即底层基于链表的线性表

当业务中查询少,但需要频繁增,删的场合可使用LinkedList替代ArrayList。

特点:

  1. 底层基于链表,不存在初始化容器大小,也不用自动扩容
  2. 新增/删除元素平均时间复杂度: O(n)
  3. 查找元素平均时间复杂度:O(n)
  4. LinkedList除了作为普通的线性链表,还可以作为Java标准的栈和队列实现。所以它的接口要比ArrayList多很多。

LinkedList主要方法

作为普通链表
  1. 初始化
    通过new LinkedList() 或 new LinkedList(Collection c)初始化一个链表

  2. add(int index, E e)
    在指定索引位新增一个元素,先找到index位的结点,然后将新结点插入到指定位置即可

  3. get(int index)
    返回指定索引位的元素,实现也很简单,即从链表的头开始遍历到指定的索引位即可。 相比ArrayList的实现,它的时间复杂度比较高是O(n)

  4. remove(int index)
    删除指定索引位的元素,相比ArrayList实现比较简单,不需要移位

作为栈(stack)
  1. 初始化空栈
new LinkedList()
  1. 压栈(push)
push(E e)
  1. 出栈(pop)
pop()
  1. 取栈顶元素(peek)
peek()
作为队列(Queue)
  1. 入列(offer)
boolean offer(E e);

元素入列到队尾

  1. 出列(poll)
E poll();

元素从队首出列

  1. 取队首元素(peek)
E peek();
作为双端队列(Deque)
  1. 队首入列(offerFirst)
boolean offerFirst(E e);
  1. 队尾入列(offerLast)
boolean offerLast(E e);
  1. 队首出列(pollFirst)
E pollFirst();
  1. 队尾出列(pollLast)
E pollLast();
  1. 取队首元素(peekFirst)
E peekFirst();
  1. 取队尾元素(peekLast)
E peekLast();
源码分析
  1. add(int index, E element); E remove(int index)
    /*** Inserts the specified element at the specified position in this list.* Shifts the element currently at that position (if any) and any* subsequent elements to the right (adds one to their indices).** @param index index at which the specified element is to be inserted* @param element element to be inserted* @throws IndexOutOfBoundsException {@inheritDoc}*/public void add(int index, E element) {// 检查index范围, 0 <= index <= sizecheckPositionIndex(index);// 元素插入到末尾if (index == size)linkLast(element);else // 非末尾,插入到index所在元素的前面linkBefore(element, node(index));}/*** Removes the element at the specified position in this list.  Shifts any* subsequent elements to the left (subtracts one from their indices).* Returns the element that was removed from the list.** @param index the index of the element to be removed* @return the element previously at the specified position* @throws IndexOutOfBoundsException {@inheritDoc}* 移除操作非常简单,断开目标元素的上下指针即可*/public E remove(int index) {checkElementIndex(index);return unlink(node(index));}
  1. push, pop, peek
/*** Pushes an element onto the stack represented by this list.  In other* words, inserts the element at the front of this list.** <p>This method is equivalent to {@link #addFirst}.** @param e the element to push* @since 1.6*/public void push(E e) {addFirst(e);}/*** Pops an element from the stack represented by this list.  In other* words, removes and returns the first element of this list.** <p>This method is equivalent to {@link #removeFirst()}.** @return the element at the front of this list (which is the top*         of the stack represented by this list)* @throws NoSuchElementException if this list is empty* @since 1.6*/public E pop() {return removeFirst();}/*** Retrieves, but does not remove, the head (first element) of this list.** @return the head of this list, or {@code null} if this list is empty* @since 1.5*/public E peek() {final Node<E> f = first;return (f == null) ? null : f.item;}栈的三个操作非常简单,就是只从头这一端操作。 
  1. offer, poll, peek
/*** Adds the specified element as the tail (last element) of this list.* 入列操作即将元素加入到队尾* @param e the element to add* @return {@code true} (as specified by {@link Queue#offer})* @since 1.5*/public boolean offer(E e) {return add(e);}/*** Retrieves and removes the head (first element) of this list.* 出列操作即从队首取出元素* @return the head of this list, or {@code null} if this list is empty* @since 1.5*/public E poll() {final Node<E> f = first;return (f == null) ? null : unlinkFirst(f);}/*** Retrieves, but does not remove, the head (first element) of this list.* 获取队首元素,即取首元素* @return the head of this list, or {@code null} if this list is empty* @since 1.5*/public E peek() {final Node<E> f = first;return (f == null) ? null : f.item;}
  1. offerFirst, offerLast, pollFirst, pollLast, peekFirst, peekLast
    双端队列的操作
/*** Inserts the specified element at the front of this list.** @param e the element to insert* @return {@code true} (as specified by {@link Deque#offerFirst})* @since 1.6*/public boolean offerFirst(E e) {addFirst(e);return true;}/*** Inserts the specified element at the end of this list.** @param e the element to insert* @return {@code true} (as specified by {@link Deque#offerLast})* @since 1.6*/public boolean offerLast(E e) {addLast(e);return true;}/*** Retrieves, but does not remove, the first element of this list,* or returns {@code null} if this list is empty.** @return the first element of this list, or {@code null}*         if this list is empty* @since 1.6*/public E peekFirst() {final Node<E> f = first;return (f == null) ? null : f.item;}/*** Retrieves, but does not remove, the last element of this list,* or returns {@code null} if this list is empty.** @return the last element of this list, or {@code null}*         if this list is empty* @since 1.6*/public E peekLast() {final Node<E> l = last;return (l == null) ? null : l.item;}/*** Retrieves and removes the first element of this list,* or returns {@code null} if this list is empty.** @return the first element of this list, or {@code null} if*     this list is empty* @since 1.6*/public E pollFirst() {final Node<E> f = first;return (f == null) ? null : unlinkFirst(f);}/*** Retrieves and removes the last element of this list,* or returns {@code null} if this list is empty.** @return the last element of this list, or {@code null} if*     this list is empty* @since 1.6*/public E pollLast() {final Node<E> l = last;return (l == null) ? null : unlinkLast(l);}// 双端队列的操作非常简单,队首和队尾同时支持入列,出列和取元素

http://www.ppmy.cn/news/1207421.html

相关文章

基于安卓android微信小程序的物流仓储系统

项目介绍 本文以实际运用为开发背景&#xff0c;运用软件工程原理和开发方法&#xff0c;它主要是采用java语言技术和mysql数据库来完成对系统的设计。整个开发过程首先对物流仓储系统进行需求分析&#xff0c;得出物流仓储系统主要功能。接着对物流仓储系统进行总体设计和详细…

苹果Ios系统app应用程序开发者如何获取IPA文件签名证书时需要注意什么?

今天呢想和大家介绍介绍苹果App开发者如何获取IPA文件签名证书的步骤和注意事项。对于苹果应用程序开发者而言&#xff0c;获取IPA文件签名证书是发布应用程序至App Store的重要步骤之一。签名证书能够确保应用程序的安全性和可信度&#xff0c;并使其能够在设备上正确运行。 …

tqdm学习

from tqdm import tqdmepochs 10 epoch_bar tqdm(range(epochs)) count 0 for _ in epoch_bar:count count1print("count {}".format(count))print(_)每次就是一个epoch

CSDN每日一题学习训练——Java版(克隆图、最接近的三数之和、求公式的值)

版本说明 当前版本号[20231109]。 版本修改说明20231109初版 目录 文章目录 版本说明目录克隆图题目解题思路代码思路参考代码 最接近的三数之和题目解题思路代码思路参考代码 求公式的值题目解题思路代码思路参考代码 克隆图 题目 给你无向 连通(https://baike.baidu.com…

语音控制:基于ESP8266的DIY助手

随着智能家居的兴起&#xff0c;语音控制成为越来越受欢迎的方式。在本篇文章中&#xff0c;我们将向您介绍如何使用ESP8266构建一个基于语音控制的DIY助手。 第一步&#xff1a;硬件准备 在开始之前&#xff0c;您需要准备一些基础硬件&#xff0c;包括ESP8266模块、麦克风模…

C#开发的OpenRA游戏之延时初始化Lazy<T> 类

C#开发的OpenRA游戏之延时初始化Lazy<T> 类 Lazy<T> 是一个类,用于实现延时加载(Lazy Initialization)。延时加载是指对象的创建被推迟,直到第一次被使用时。Lazy<T> 允许你在第一次访问对象时进行初始化,这对于大型或资源密集型对象的性能优化非常有用…

uboot 和 内存地址

前言 在使用 uboot 升级的时候&#xff0c;有个疑问&#xff1a; 通过 tftp 下载的 bin 文件&#xff0c;我该暂存在哪段内存空间&#xff1f;换句话说&#xff0c;哪段内存空间可供我存放临时数据&#xff1f; 带着这个疑问&#xff0c;开启今天的 uboot 和 内存地址 研究之旅…

【每日一题】咒语和药水的成功对数

文章目录 Tag题目来源解题思路方法一&#xff1a;排序二分 写在最后 Tag 【排序二分】【数组】【2023-11-10】 题目来源 2300. 咒语和药水的成功对数 解题思路 方法一&#xff1a;排序二分 我们首先对 points 进行升序排序&#xff0c;然后枚举 spells 中的 x&#xff0c;需…