简述

 按照上篇笔记ArrayList集合继续进行介绍list的另一个常见子类LinkedList

?LinkedList介绍

1.数据结构

LinkedList集合(JDK1.8)

说明:linkedlist的底层数据结构是个双向链表结构,也意味着linkedlist在进行查询时效率会比ArrayList的慢,而插入和删除只是对指针进行移动,相对于ArrayList就会快很多

2.源码分析

2.1类的继承关系

 

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

 

2.2类的属性

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
    // 实际元素个数
    transient int size = 0;
    // 头结点
    transient Node<E> first;
    // 尾结点
    transient Node<E> last;
}

private static class Node<E> {
        E item; // 数据域
        Node<E> next; // 后继
        Node<E> prev; // 前驱
        
        // 构造函数,赋值前驱后继
        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

说明:linkedlist的结构中,一个头结点,一个尾节点,一个表示链表中实际元素个数的变量。注意,头结点、尾结点都有transient关键字修饰,这也意味着在序列化时该域是不会序列化的。

2.3构造函数

public LinkedList() {
}

public LinkedList(Collection<? extends E> c) {
        // 调用无参构造函数
        this();
        // 添加集合中所有的元素
        addAll(c);
    }

说明:会调用无参构造函数,并且会把集合中所有的元素添加到LinkedList中。

2.4核心函数

1.add函数

 public boolean add(E e) {
        // 添加到末尾
        linkLast(e);
        return true;
    }

void linkLast(E e) {
        // 保存尾结点,l为final类型,不可更改
        final Node<E> l = last;
        // 新生成结点的前驱为l,后继为null
        final Node<E> newNode = new Node<>(l, e, null);
        // 重新赋值尾结点
        last = newNode;    
        if (l == null) // 尾结点为空
            first = newNode; // 赋值头结点
        else // 尾结点不为空
            l.next = newNode; // 尾结点的后继为新生成的结点
        // 大小加1    
        size++;
        // 结构性修改加1
        modCount++;
    }

举个栗子

LinkedList集合(JDK1.8)

说明:初始化状态效果

 LinkedList集合(JDK1.8)

说明:linkedlist允许传入值是重复的,并且也允许为null

 

 2.add(int index, E element)函数

// 插入元素
public void add(int index, E element) {
    checkPositionIndex(index);  // 检查是否越界
    if (index == size)          // 在链表末尾添加
        linkLast(element);
    else                        // 在链表中间添加
        linkBefore(element, node(index));
}
void linkBefore(E e, Node<E> succ) { final Node<E> pred = succ.prev; final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; }

说明:通过先判断index的合法性,然后再与size进行比较,如果等于size的话,就相当于直接调用了addlast方法,若不是,则进行中间插入操作。

 

3.addAll函数

public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }

// 添加一个集合
public boolean addAll(int index, Collection<? extends E> c) {
        // 检查插入的的位置是否合法
        checkPositionIndex(index);
        // 将集合转化为数组
        Object[] a = c.toArray();
        // 保存集合大小
        int numNew = a.length;
        if (numNew == 0) // 集合为空,直接返回
            return false;

        Node<E> pred, succ; // 前驱,后继
        if (index == size) { // 如果插入位置为链表末尾,则后继为null,前驱为尾结点
            succ = null;
            pred = last;
        } else { // 插入位置为其他某个位置
            succ = node(index); // 寻找到该结点
            pred = succ.prev; // 保存该结点的前驱
        }

        for (Object o : a) { // 遍历数组
            @SuppressWarnings("unchecked") E e = (E) o; // 向下转型
            // 生成新结点
            Node<E> newNode = new Node<>(pred, e, null);
            if (pred == null) // 表示在第一个元素之前插入(索引为0的结点)
                first = newNode;
            else
                pred.next = newNode;
            pred = newNode;
        }

        if (succ == null) { // 表示在最后一个元素之后插入
            last = pred;
        } else {
            pred.next = succ;
            succ.prev = pred;
        }
        // 修改实际元素个数
        size += numNew;
        // 结构性修改加1
        modCount++;
        return true;
    }

说明:addAll有两个重载函数,addAll(Collection<? extends E>)型和addAll(int, Collection<? extends E>)型,我们平时习惯调用的addAll(Collection<? extends E>)型会转化为addAll(int, Collection<? extends E>)型。

         参数中的index表示在索引下标为index的结点(实际上是第index + 1个结点)的前面插入。在addAll函数中,addAll函数中还会调用到node函数,get函数也会调用到node函数,此函数是根据索引下标找到该结点并返回,具体代码如下

Node<E> node(int index) {
        // 判断插入的位置在链表前半段或者是后半段
        if (index < (size >> 1)) { // 插入位置在前半段
            Node<E> x = first; 
            for (int i = 0; i < index; i++) // 从头结点开始正向遍历
                x = x.next;
            return x; // 返回该结点
        } else { // 插入位置在后半段
            Node<E> x = last; 
            for (int i = size - 1; i > index; i--) // 从尾结点开始反向遍历
                x = x.prev;
            return x; // 返回该结点
        }
    }

说明:在根据索引查找结点时,会有一个小优化,结点在前半段则从头开始遍历,在后半段则从尾开始遍历,这样就保证了只需要遍历最多一半结点就可以找到指定索引的结点。

 

4.indexOf函数

public int indexOf(Object o) {
        int index = 0;
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null)
                    return index;
                index++;
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item))
                    return index;
                index++;
            }
        }
        return -1;
    }
View Code

相关文章:

  • 2021-08-23
  • 2022-12-23
  • 2021-08-31
  • 2021-12-05
  • 2022-01-06
  • 2021-04-03
  • 2021-11-30
猜你喜欢
  • 2022-01-14
  • 2022-12-23
  • 2021-09-17
相关资源
相似解决方案