Java中的链表——LinkedList解析

LinkedList类结构

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

Node结构
双向链表结构(指向前一结点和后一节点的引用)

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

声明变量

    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node first;

    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node last;

链表添加首位
推导–>(链表添加末尾)
同理。可得到添加addBefore()上一元素,根据条件不同

/**private void addLast(E e){*/
private void andFirst(E e) {
        /**final Node l = last;*/
        final Node f = first; 
        //assert:last.next == null && first.item != null;
        /**final Node newNode = new Node<>(l,e,null);*/
        //assert:first.prev == null && first.item != null
        //init newNode
        final Node newNode = new Node<>(null, e, f);
        /**last = newNode*/
        first = newNode; 
        //如果首位结点为空,则直接添加。否则添加到结点元素的上一结点
        /** if(l == null) first = newNode else l.next = newNode */
        if (f == null) 
            last = newNode;
        else
            f.prev = newNode;
        size++; // init size=0
        modCount++;
}

添加上一元素

private void addBefore(E e,Node target){
//assert: node.next = target && node.prev = target.prev != null
    Node newNode = (target.prev,e,target);
}

删除首元素,并且返回
推导–>删除末尾元素,并且返回下一个元素
如果需要删除任意一个未知结点Node X,需要判断两种极端情况(最末或者最前,然后往后(往前)推,当前位置清空回收

//private E removeLast(Node f){
private E removeFirst(Node f) {
        //assert f == first && f != null;
        final E element = f.item; //得到该元素
        //final Node prev = f.prev;
        final Node next = f.next; //得到它下一个引用
        f.item = null; //清空
        //f.prev = null;
        f.next = null; // 回收内存(GC)
        //last = prev;
        first = next; //将下一个Node移到首位
        //if(prev == null) first= null; else prev.next = null
        //如果为空,则代表List只有一个元素,回收
        //否则,移除该元素
        if (next == null) 
            last = null;
        else
            next.prev = null;
        size--;
        modCount++;
        return element;
}
E remove(Node<E> x) {
        // assert x != null;
        final E element = x.item;
        final Node<E> next = x.next;
        final Node<E> prev = x.prev;

        if (prev == null) {
            first = next;
        } else { //填充在后一元素的位置
            prev.next = next;
            x.prev = null; //之前的位置清空回收
        }

        if (next == null) { 
            last = prev;
        } else { //填充在前一元素的位置
            next.prev = prev;
            x.next = null; //之后的位置清空回收
        }

        x.item = null;
        size--;
        modCount++;
        return element;
}

你可能感兴趣的:(Java源码解析,算法结构,linkedlist,链表,java)