Java源码阅读-LinkedList

来源:互联网 发布:js 对象添加属性和值 编辑:程序博客网 时间:2024/06/13 21:21
    transient int size = 0; // 数组长度,transient关键字,序列化的时候忽略该成员    /**     * 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() {    }

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


添加元素

/**     * 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); // 直接调用inkLast方法        return true;    }

/**     * Links e as last element.     */    void linkLast(E e) { // 直接插入到最后,所以时间复杂度是O(1)        final Node<E> l = last;        final Node<E> newNode = new Node<>(l, e, null); // 新的节点,前一节点是插入前的最后一个节点,后一节点为null        last = newNode; // 插入后最后一个节点为新的节点        if (l == null) // 插入的是第一个节点,所以也是第一个节点            first = newNode;        else            l.next = newNode;        size++;        modCount++;    }


判断是否包含元素

/**     * 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 ? e==null : 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) { // 判断元素是否存在,调用indexOf函数        return indexOf(o) != -1;    }

public int indexOf(Object o) { // 查找元素的位置,需要遍历,所以时间复杂度是O(n)        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;    }


删除元素

/**     * Removes the first occurrence of the specified element from this list,     * if it is present.  If this list does not contain the element, it is     * unchanged.  More formally, removes the element with the lowest index     * {@code i} such that     * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>     * (if such an element exists).  Returns {@code true} if this list     * contained the specified element (or equivalently, if this list     * changed as a result of the call).     *     * @param o element to be removed from this list, if present     * @return {@code true} if this list contained the specified element     */    public boolean remove(Object o) {        if (o == null) {            for (Node<E> x = first; x != null; x = x.next) {                if (x.item == null) {                    unlink(x);                    return true;                }            }        } else {            for (Node<E> x = first; x != null; x = x.next) { // 遍历链表找到该元素                if (o.equals(x.item)) {                    unlink(x);                    return true;                }            }        }        return false;    }
/**     * 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;        } 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;    }





原创粉丝点击