当我们在聊LinkedList

本文出自:https://blog.csdn.net/DT235201314/article/details/80421037

一丶概述

上篇说到ArrayList及源码,这篇说说LinkedList及源码

二丶正文

1.目录图


2.ArrayList与LinkedList区别

(1)数据结构

ArrayList,List()接口,列表,想象结构如下图:


底层为数组,单向。

LikedList实现List()接口,Deque接口,双向链表,想象结构如下:


底层为内部实体类,双向链表。

(2)复杂度

空间复杂度:LinledList装的实体类,且有正反向“指针参数”,大于ArrayList。

时间复杂度:LinkedList继承了AbstractSequentialList并且实现List,Deque,Cloneable, Serializable接口。
其中,AbstractSequentialList相较于AbstractList(ArrayList的父类),只支持次序访问,而不支持随机访问,因为它的

get(int index) ,set(int index, E element), add(int index, E element), remove(int index) 都是基于迭代器实现的。所以在LinkedList使用迭代器遍历更快。

get/set ArrayListO(1)<LinkedListO(n)增删操作 Array..O(n)>LinkedO(1)

3.源码分析

(1)继承关系:

public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable
1.从图中我可以看出LinkedList实现了Deque接口,可以将LinkedList当做队列使用;实现了cloneable表示能被克隆,实现了Serializable接口表示支持序列化;
2.LinkedList基于双向链表,实现了所有List操作并允许所有元素包括null值,它可以被当作双端队列;
3.LinkedList顺序访问非常高效,而随机访问效率很低;
4.LinkedList线程不安全,可以用Collections.synchronizedList使其线程安全。

(2)相关字段

/**
 * LinkedList长度,不可序列化
 */
transient int size = 0;

/**
 * 首结点
 */
transient Node<E> first;

/**
 * 尾结点
 */
transient Node<E> last;
/**
 * 内部实体类,泛型数值与正反向“指针”参数标签
 * @param <E>
 */
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;
    }
}

(3)构造方法

/**
 * Constructs an empty list.
 */
public LinkedList() {
}

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

(4)方法介绍

Creat(创造添加)方法

LinkedList新增(public)有如下几个方法:
public boolean add(E e);
public boolean offer(E e);

public void addLast(E e);

public boolean offerLast(E e);

/**
 * 末尾插入元素
 */
public boolean add(E e) {
    linkLast(e);
    return true;
}

public boolean offer(E e) {
    // 尾插元素
    return add(e);
}

/**
 * 尾插元素
 */
public void addLast(E e) {
    linkLast(e);
}

/**
 * 尾插元素
 */
public boolean offerLast(E e) {
    addLast(e);
    return true;
}

/**
 * 尾插元素
 */
void linkLast(E e) {
    //获取当前尾结点
    final Node l = last;
    //定义新结点,其前驱结点为尾结点,值为e,后继结点为null
    final Node newNode = new Node<>(l, e, null);
    //将刚定义的新节点设为尾结点
    last = newNode;
    //若原尾结点为null,即原链表为null,则链表首结点也为newNode
    if (l == null)
        first = newNode;
        //若不是原尾结点的后继设为newNode
    else
        l.next = newNode;
    //长度+1
    size++;
    //改变次数+1
    modCount++;
}
核心方法linkLast

头部插入方法:

public boolean offerFirst(E e);
public void addFirst(E e);
public void push(E e);

/**
 * 头插指定元素
 */
public void addFirst(E e) {
    linkFirst(e);
}
public void push(E e) {
    addFirst(e);
}
/**
 * 头插指定元素
 */
public boolean offerFirst(E e) {
    addFirst(e);
    return true;
}
/**
 * 头插
 */
private void linkFirst(E e) {
    //获取当前首结点
    final Node f = first;
    //定义新结点,前驱为null,结点值为e,后驱为f
    final Node newNode = new Node<>(null, e, f);
    //将newNode设为首结点
    first = newNode;
    //若首结点为null,尾结点设为newNode,若不是原首结点前驱设为newNode
    if (f == null)
        last = newNode;
    else
        f.prev = newNode;
    //长度+1
    size++;
    modCount++;
}

正常添加:

public void add(int index, E element);
public boolean addAll(Collection<? extends E> c);
public boolean addAll(int index, Collection<? extends E> c);

/**
 * 指定位置插入元素
 */
public void add(int index, E element) {
    // 判断索引是否越界
    checkPositionIndex(index);
    // 若指定位置在尾部,则尾插元素;若不在调通用方法指定位置插入
    if (index == size)
        linkLast(element);
    else
        linkBefore(element, node(index));
}
/**
 * 尾插集合所有元素
 */
public boolean addAll(Collection c) {
    return addAll(size, c);
}
/**
 * 指定位置插入集合所有元素
 */
public boolean addAll(int index, Collection c) {
    //判断索引是否越界
    checkPositionIndex(index);
    //将集合转为数组
    Object[] a = c.toArray();
    //获取数组长度
    int numNew = a.length;
    //若数组长度为0,即没有要插入的元素返回false
    if (numNew == 0)
        return false;
    //succ为原index位置上结点,pred为succ前驱结点    
    Node pred, succ;
    //若是尾部,succ为null,pred为尾结点
    if (index == size) {
        succ = null;
        pred = last;
        //若不是succ为index位置结点,pred为其前驱结点    
    } else {
        succ = node(index);
        pred = succ.prev;
    }
    //for循环遍历集合
    for (Object o : a) {
        @SuppressWarnings("unchecked") E e = (E) o;
        //定义一个新结点,其前驱结点为pred,结点值为e,后继结点为null
        Node newNode = new Node<>(pred, e, null);
        //若前驱结点为null,则将newNode设为首结点
        if (pred == null)
            first = newNode;
        else
            //若存在前驱结点,将其后继赋为newNode
            pred.next = newNode;
        //类似于迭代器的next,将newNode指向下一个需插入结点位置的前驱
                pred = newNode;
    }
    //若是尾插,则最后添加元素为尾结点
    if (succ == null) {
        last = pred;
    } else {
        //若不是,最后添加的结点后继指向原index位置上的succ
        pred.next = succ;
        //succ的前驱指向最后添加的结点    
        succ.prev = pred;
    }
    //长度+numNew
    size += numNew;
    modCount++;
    return true;
}

private void checkPositionIndex(int index) {
    //若index不在[0,size]区间内则抛越界异常
    if (!isPositionIndex(index))
        throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private boolean isPositionIndex(int index) {
    return index >= 0 && index <= size;
}
/**
 * 获取指定位置结点
 */
Node node(int index) {
    // 若指定索引小于LinkedList长度一半,则从首结点开始遍历;若不是从尾结点开始遍历
    if (index < (size >> 1)) {
        Node x = first;
        for (int i = 0; i < index; i++)
            x = x.next;
        return x;
    } else {
        Node x = last;
        for (int i = size - 1; i > index; i--)
            x = x.prev;
        return x;
    }
}
/**
 * 中间插入
 */
void linkBefore(E e, Node succ) {
    // 获取原索引上元素前驱结点pred 
    final Node pred = succ.prev;
    // 定义新结点,其前驱结点为pred,结点值为e,后继为succ
    final Node newNode = new Node<>(pred, e, succ);
    // 将succ的前驱结点设为newNode
    succ.prev = newNode;
    // pred为空,即succ为原首结点,那么将新定义的newNode设为新首结点
    if (pred == null)
        first = newNode;
    else
        //否则pred后继结点设为newNode
        pred.next = newNode;
    //长度+1    
    size++;
    //修改次数+1
    modCount++;
}

双向表添加图示:


Updata (修改)方法

public E set(int index, E element);

/**
 * 修改指定位置结点值,返回被替换结点值
 */
public E set(int index, E element) {
    //校验index是否越界
    checkElementIndex(index);
    //获取当前index位置上结点
    Node x = node(index);
    //获取此结点值
    E oldVal = x.item;
    //修改结点值
    x.item = element;
    //返回被替换结点值
    return oldVal;
}

Delete(删除)方法:

public E remove();
public E remove(int index);
public boolean remove(Object o);
public E removeFirst();
public boolean removeFirstOccurrence(Object o);
public E removeLast();
public boolean removeLastOccurrence(Object o);
public void clear();
public E poll();
public E pollFirst();
public E pop();

/**
 * 获取并删除首结点值,若链表为空则抛出异常
 */
public E remove() {
    return removeFirst();
}

public E pop() {
    return removeFirst();
}

public E removeFirst() {
    final Node f = first;
    if (f == null)
        throw new NoSuchElementException();
    return unlinkFirst(f);
}

/**
 * 获取并删除首结点,若链表为空返回null
 */
public E poll() {
    final Node f = first;
    return (f == null) ? null : unlinkFirst(f);
}

public E pollFirst() {
    final Node f = first;
    return (f == null) ? null : unlinkFirst(f);
}

private E unlinkFirst(Node f) {
    //获取首结点值
    final E element = f.item;
    //获取首结点后继
    final Node next = f.next;
    //删除首结点
    f.item = null;
    f.next = null; // help GC
    //原后继结点设为首结点
    first = next;
    //若后继结点为null,尾结点设为null;若不是后继结点的前驱结点设为null
    if (next == null)
        last = null;
    else
        next.prev = null;
    //长度-1    
    size--;
    modCount++;
    return element;
}

/**
 * 删除指定位置结点
 */
public E remove(int index) {
    //校验index是否越界
    checkElementIndex(index);
    return unlink(node(index));
}

/**
 * 删除指定结点
 */
E unlink(Node x) {
    // 获取指定结点值
    final E element = x.item;
    // 获取指定结点后继
    final Node next = x.next;
    // 获取指点结点前驱
    final Node prev = x.prev;
    // 若前驱为null,则后继结点设为首结点
    if (prev == null) {
        first = next;
    } else {
        //若不是,指定结点后继结点设为指定结点前驱结点后继,指定结点的前驱设为null
        prev.next = next;
        x.prev = null;
    }
    //若后继结点为空,则前驱结点设为尾结点
    if (next == null) {
        last = prev;
    } else {
        //若不是,指定结点前驱结点设为指定结点后继结点前驱,指定结点的后继设为null
        next.prev = prev;
        x.next = null;
    }
    //指定结点值设为null
    x.item = null;
    //长度-1
    size--;
    modCount++;
    //返回被删除的结点值
    return element;
}

/**
 * for循环从头遍历,删除第一次出现的指定元素
 */
public boolean remove(Object o) {
    if (o == null) {
        for (Node x = first; x != null; x = x.next) {
            if (x.item == null) {
                unlink(x);
                return true;
            }
        }
    } else {
        for (Node x = first; x != null; x = x.next) {
            if (o.equals(x.item)) {
                unlink(x);
                return true;
            }
        }
    }
    return false;
}

/**
 * 删除从头开始第一出现的指定结点
 */
public boolean removeFirstOccurrence(Object o) {
    return remove(o);
}

/**
 * 删除尾结点,若链表为空抛异常
 */
public E removeLast() {
    final Node l = last;
    if (l == null)
        throw new NoSuchElementException();
    return unlinkLast(l);
}

/**
 * 删除尾结点,若链表为空返回null
 */
public E pollLast() {
    final Node l = last;
    return (l == null) ? null : unlinkLast(l);
}

/**
 * 删除尾结点
 */
private E unlinkLast(Node l) {
    // 获取尾结点值
    final E element = l.item;
    // 获取尾结点前驱
    final Node prev = l.prev;
    // 删除尾结点
    l.item = null;
    l.prev = null; // help GC
    // 将原尾结点前驱设为尾结点
    last = prev;
    // 若原尾结点的前驱结点为空,则首结点设为null
    if (prev == null)
        first = null;
    else
        //若不为null,原原尾结点的前驱结点的后继结点设为null
        prev.next = null;
    //长度-1    
    size--;
    modCount++;
    return element;
}

/**
 * 删除最后出现的结点
 */
public boolean removeLastOccurrence(Object o) {
    if (o == null) {
        for (Node x = last; x != null; x = x.prev) {
            if (x.item == null) {
                unlink(x);
                return true;
            }
        }
    } else {
        for (Node x = last; x != null; x = x.prev) {
            if (o.equals(x.item)) {
                unlink(x);
                return true;
            }
        }
    }
    return false;
}

/**
 * 清空LinkedList
 */
public void clear() {
    //循环遍历LinkedList,删除所有结点
    for (Node x = first; x != null; ) {
        Node next = x.next;
        x.item = null;
        x.next = null;
        x.prev = null;
        x = next;
    }
    //首尾结点置空
    first = last = null;
    //长度设为0
    size = 0;
    modCount++;
}

删除图解:

四丶参考文章

Java集合——LinkedList

写在最后微信扫码提问


猜你喜欢

转载自blog.csdn.net/dt235201314/article/details/80421037