数据结构 - LinkedList 类(特殊线性集合)

简介

为什么说它特殊,从类图上就可以看出来,它是List、Queue(Deque继承Queue)的实现,相比ArrayList多实现了Deque接口而少实现了RandomAccess接口,从名字就可以看除内部使用链表结构

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

实现List , Deque 接口,继承AbstractSequentialList 抽象类

AbstractSequentialList 抽象类
public abstract class AbstractSequentialList<E> extends AbstractList<E>

AbstractSequentialList继承自AbstractList。AbstractSequentialList是在迭代器的基础上实现的get、set、add和remove方法。并且AbstractSequentialList 只支持按次序访问,而不像 AbstractList 那样支持随机访问

AbstractSequentialList 构造函数
protected AbstractSequentialList() {
}
AbstractSequentialList 方法
// 根据位置取元素
public E get(int index) {
    try {
        return listIterator(index).next();
    } catch (NoSuchElementException exc) {
        throw new IndexOutOfBoundsException("Index: "+index);
    }
}

通过listIterator确定位置,调用next取值

// 修改指定位置的元素为新值
public E set(int index, E element) {
    try {
        ListIterator<E> e = listIterator(index);
        E oldVal = e.next();
        e.set(element);
        return oldVal;
    } catch (NoSuchElementException exc) {
        throw new IndexOutOfBoundsException("Index: "+index);
    }
}

覆盖新值返回旧值

// 添加元素到指定位置
public void add(int index, E element) {
    try {
        listIterator(index).add(element);
    } catch (NoSuchElementException exc) {
        throw new IndexOutOfBoundsException("Index: "+index);
    }
}

如果指定位置有值,将当前处于该位置的后续元素移到右边

// 删除
public E remove(int index) {
    try {
        ListIterator<E> e = listIterator(index);
        E outCast = e.next();
        e.remove();
        return outCast;
    } catch (NoSuchElementException exc) {
        throw new IndexOutOfBoundsException("Index: "+index);
    }
}

listIterator 取值,调用listIterator删除元素

// 在指定位置添加一个线性集合
public boolean addAll(int index, Collection<? extends E> c) {
    try {
        boolean modified = false;
        ListIterator<E> e1 = listIterator(index);
        Iterator<? extends E> e2 = c.iterator();
        while (e2.hasNext()) {
            e1.add(e2.next());
            modified = true;
        }
        return modified;
    } catch (NoSuchElementException exc) {
        throw new IndexOutOfBoundsException("Index: "+index);
    }
}
// 获取迭代器
public Iterator<E> iterator() {
    return listIterator();
}
// 根据索引获取listIterator
public abstract ListIterator<E> listIterator(int index);

AbstractSequencetialList中的增删改查都是用ListIterator完成,子类必须实现listIterator(int index)方法

重要内部类Node
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;
    }
}

Node 用来包装实际元素值,并且提供两个指针prev、next。

LinkedList 属性
// 长度
transient int size = 0;
// 头元素
transient Node<E> first;
// 尾元素
transient Node<E> last;
LinkedList 构造函数
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
    this();
    addAll(c);
}

从构造函数可以看出初始默认值为0

LinkedList 双向链表基础方法
// 在头上增加元素
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;
    // 长度加1
    size++;
    // 修改次数加1
    modCount++;
}
// 在尾部追加元素
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;
    // 长度加1
    size++;
    // 修改次数加1
    modCount++;
}
// 在succ节点前插入元素
void linkBefore(E e, Node<E> succ) {
    final Node<E> pred = succ.prev;
    // 构建新元素,新节点下级指向succ
    final Node<E> newNode = new Node<>(pred, e, succ);
    // succ上一个节点指向新节点
    succ.prev = newNode;
    // succ原来上级节点的next指向新节点
    if (pred == null)
        first = newNode;
    else
        pred.next = newNode;
    // 长度加1
    size++;
    // 修改次数加1
    modCount++;
}
// 删除头节点
private E unlinkFirst(Node<E> f) {
    final E element = f.item;
    // 获取下一个节点
    final Node<E> next = f.next;
    f.item = null;
    f.next = null; // help GC
    // 头节点指向next
    first = next;
    if (next == null)
        last = null;
    else
        next.prev = null;
    // 长度减1
    size--;
    // 修改次数加1
    modCount++;
    return element;
}
// 删除尾节点
private E unlinkLast(Node<E> l) {
    final E element = l.item;
    // 获取上一个节点
    final Node<E> prev = l.prev;
    l.item = null;
    l.prev = null; // help GC
    // 尾节点指向上一个节点
    last = prev;
    if (prev == null)
        first = null;
    else
        prev.next = null;
    // 长度减1
    size--;
    // 修改次数加1
    modCount++;
    return element;
}
// 删除节点
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;
}
// 索引取值
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;
    }
}
LinkedList 添加
// 添加元素
public boolean add(E e) {
    // 默认尾部追加
    linkLast(e);
    return true;
}
// 头部添加
public void addFirst(E e) {
    linkFirst(e);
}
// 尾部添加
public void addLast(E e) {
    linkLast(e);
}
LinkedList 删除
// 移除头元素
public E removeFirst() {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
    return unlinkFirst(f);
}
// 移除尾元素
public E removeLast() {
    final Node<E> l = last;
    if (l == null)
        throw new NoSuchElementException();
    return unlinkLast(l);
}
// 删除元素
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 {
        // 遍历链表找到equals一样的元素
        for (Node<E> x = first; x != null; x = x.next) {
            if (o.equals(x.item)) {
                unlink(x);
                // 只会删除第一个
                return true;
            }
        }
    }
    return false;
}
LinkedList 搜索
// 获取头节点
public E getFirst() {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
    return f.item;
}
// 获取尾节点
public E getLast() {
    final Node<E> l = last;
    if (l == null)
        throw new NoSuchElementException();
    return l.item;
}
// 根据所有取值
public E get(int index) {
    checkElementIndex(index);
    // 调用node(index)
    return node(index).item;
}

猜你喜欢

转载自www.cnblogs.com/yuanjiangnan/p/12643408.html
今日推荐