LinkedList源码

一些参数:

    transient int size = 0;  // 集合大小
    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first; // 第一个节点
    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last;  //  最后一个节点

构造函数

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

    /**
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param  c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

这里可以看出linkedList是一个双向链表:

// 结点
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;
    }
}

常用的一些方法

是否含有该对象:

    /**
     * Returns {@code true} if this list contains the specified element.
     * More formally, returns {@code true} if and only if this list contains
     * at least one element {@code e} such that
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
     *
     * @param o element whose presence in this list is to be tested
     * @return {@code true} if this list contains the specified element
     */
    public boolean contains(Object o) {
        return indexOf(o) != -1;
    }


 public int indexOf(Object o) {
    int index = 0;
    // o 为null的时候
    if (o == null) {
        // 遍历整个链表
        for (Node<E> x = first; x != null; x = x.next) {
            if (x.item == null)
                return index;  // 返回对象所在的位置
            index++;
        }
    } else {
        // o 不为null的时候
        // 遍历
        for (Node<E> x = first; x != null; x = x.next) {
            if (o.equals(x.item))
                return index;
            index++;
        }
    }
    // 没有找到
    return -1;
}

添加:

    /**
     * Appends the specified element to the end of this list.
     *
     * <p>This method is equivalent to {@link #addLast}.
     *
     * @param e element to be appended to this list
     * @return {@code true} (as specified by {@link Collection#add})
     */
    public boolean add(E e) {
        linkLast(e);
        return true;
    }

    /**
     * Links e as last element.
     */
    void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null); // 创建结点 没有后结点
        last = newNode;
        // 如果没有尾结点
        if (l == null)
            first = newNode; // 就是头结点
        else
            l.next = newNode;
        size++;
        modCount++; // fast - fail
    }

清空:

     /**
     * Removes all of the elements from this list.
     * The list will be empty after this call returns.
     */
    public void clear() {
        // Clearing all of the links between nodes is "unnecessary", but:
        // - helps a generational GC if the discarded nodes inhabit
        //   more than one generation 帮助gc的工作
        // - is sure to free memory even if there is a reachable Iterator 即使有迭代器可达
        for (Node<E> x = first; x != null; ) {
            Node<E> next = x.next;
            x.item = null;
            x.next = null;
            x.prev = null;
            x = next;
        }
        first = last = null;
        size = 0;
        modCount++;
    }

GET

     public E get(int index) {
        checkElementIndex(index); // 检验边界
        return node(index).item;
    }

    /**
     * Returns the (non-null) Node at the specified element index.
     * 返回指定的结点
     */
    Node<E> node(int index) {
        // assert isElementIndex(index);

        // 这里有优化,当index小于size的一半
        // 从第一个向后遍历,否则从后向前遍历
        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;
        }
    }

移除:

     public E remove(int index) {
        checkElementIndex(index);
        return unlink(node(index));
    }

     /**
     * Unlinks non-null node x.
     * 返回移除的结点
     */
    E unlink(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;  // x 为头结点,删除后next成为头结点
        } else {
            prev.next = next; // 略过 x
            x.prev = null;  // 
        }

        if (next == null) {
            last = prev; // x是尾结点,删除后 pre是最后一个
        } else {
            next.prev = prev;
            x.next = null;
        }

        x.item = null;  //   x.pre next item = null 让gc回收
        size--;
        modCount++; // fast - fail
        return element;
    }

新迭代器: 可向前向后

public ListIterator<E> listIterator(int index) {
    checkPositionIndex(index);
    return new ListItr(index);
}


public E previous() {
    checkForComodification();
    if (!hasPrevious())
        throw new NoSuchElementException();

    lastReturned = next = (next == null) ? last : next.prev;
    nextIndex--;
    return lastReturned.item;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值