Java 源碼--LinkedList

LinkedList類繼承了AbstractSequentialList抽象類,實現了List、Deque、Cloneable、Serializable接口。由此可以看出,LinkedList也是一種雙端隊列。

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

LinkedList類有三個屬性,size表示大小,first表示鏈表的第一個元素,last表示鏈表的最後一個元素。

transient int size = 0;
transient Node<E> first;
transient Node<E> last;

LinkedList類有兩個構造方法,有參和無參構造。無參構造就創建一個空的list,有參構造先創建一個空的list,然後將參數添加到list中。

public LinkedList() {
}

public LinkedList(Collection<? extends E> c) {
    this();
    addAll(c);
}

getFirst方法獲取第一個元素的值。

public E getFirst() {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
    return f.item;
}

getLast方法獲取最後一個元素的值。

public E getLast() {
    final Node<E> l = last;
    if (l == null)
        throw new NoSuchElementException();
    return l.item;
}

removeFirst方法移除第一個元素。unlinkFirst方法斷言參數就是list中的第一個元素且不爲null。

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

private E unlinkFirst(Node<E> f) {
    final E element = f.item;
    final Node<E> next = f.next;
    f.item = null;
    f.next = null; 
    first = next;
    if (next == null)
        last = null;
    else
        next.prev = null;
    size--;
    modCount++;
    return element;
}

removeLast方法移除list的最後一個元素。unlinkLast方法斷言參數是最後一個元素且不爲null。

public E removeLast() {
    final Node<E> l = last;
    if (l == null)
        throw new NoSuchElementException();
    return unlinkLast(l);
}

private E unlinkLast(Node<E> l) {
    final E element = l.item;
    final Node<E> prev = l.prev;
    l.item = null;
    l.prev = null; 
    last = prev;
    if (prev == null)
        first = null;
    else
        prev.next = null;
    size--;
    modCount++;
    return element;
}

addFirst方法將制定元素添加到第一個元素。

public void addFirst(E e) {
    linkFirst(e);
}

private void linkFirst(E e) {
    final Node<E> f = first;
    final Node<E> newNode = new Node<>(null, e, f);
    first = newNode;
    if (f == null)
        last = newNode;
    else
        f.prev = newNode;
    size++;
    modCount++;
}

addLast方法將制定元素添加到最後一個元素。

public void addLast(E e) {
    linkLast(e);
}

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

contains方法判斷list是否包含指定元素。

public boolean contains(Object o) {
    return indexOf(o) != -1;
}

size方法返回list的大小。

public int size() {
    return size;
}

add方法向list末尾添加一個元素。與addLast方法類似,只是這個方法返回的是布爾值。

public boolean add(E e) {
    linkLast(e);
    return true;
}

remove方法移除指定元素,unlink方法斷言參數不爲null。

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

E unlink(Node<E> x) {
    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;
}

addAll方法向list中添加一個集合。

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) {
        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)
            first = newNode;
        else
            pred.next = newNode;
        pred = newNode;
    }

    if (succ == null) {
        last = pred;
    } else {
        pred.next = succ;
        succ.prev = pred;
    }

    size += numNew;
    modCount++;
    return true;
}

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

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

lastIndexOf方法從後向前遍歷返回list中指定元素的位置。

public int lastIndexOf(Object o) {
    int index = size;
    if (o == null) {
        for (Node<E> x = last; x != null; x = x.prev) {
            index--;
            if (x.item == null)
                return index;
        }
    } else {
        for (Node<E> x = last; x != null; x = x.prev) {
            index--;
            if (o.equals(x.item))
                return index;
        }
    }
    return -1;
}

peek方法返回第一個元素的值。與getFirst方法區別是,此方法當第一個元素爲空時返回null,getFirst方法當第一個元素爲空時會拋出異常。

public E peek() {
    final Node<E> f = first;
    return (f == null) ? null : f.item;
}

element方法返回第一個元素的值。

public E element() {
    return getFirst();
}

poll方法返回並移除第一個元素。

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

remove方法移除第一個元素。

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

offer方法將元素添加到list中。

public boolean offer(E e) {
    return add(e);
}

offerFirst方法將元素添加到list中的最前面,最後返回布爾值。

public boolean offerFirst(E e) {
    addFirst(e);
    return true;
}

offerLast方法將元素添加到list中的末尾,最互返回布爾值。

public boolean offerLast(E e) {
    addLast(e);
    return true;
}

peekFirst方法返回list的第一個元素的值。

public E peekFirst() {
    final Node<E> f = first;
    return (f == null) ? null : f.item;
 }

peekLast方法返回list的最後一個元素的值。

public E peekLast() {
    final Node<E> l = last;
    return (l == null) ? null : l.item;
}

pollFirst方法返回並移除第一個元素。

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

pollLast方法返回並移除最後一個元素。

public E pollLast() {
    final Node<E> l = last;
    return (l == null) ? null : unlinkLast(l);
}

push方法在list最前面添加一個元素。

public void push(E e) {
    addFirst(e);
}

pop方法移除掉最後一個元素。

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

removeFirstOccurrence方法從前向後遍歷移除指定元素。

public boolean removeFirstOccurrence(Object o) {
    return remove(o);
}

removeLastOccurrence方法從後向前遍歷移除指定元素。

public boolean removeLastOccurrence(Object o) {
    if (o == null) {
        for (Node<E> x = last; x != null; x = x.prev) {
            if (x.item == null) {
                unlink(x);
                return true;
            }
        }
    } else {
        for (Node<E> x = last; x != null; x = x.prev) {
            if (o.equals(x.item)) {
                unlink(x);
                return true;
            }
        }
    }
    return false;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章