如何用源码分析LinkedList

发布时间:2021-10-20 16:44:49 作者:柒染
来源:亿速云 阅读:145

这篇文章将为大家详细讲解有关如何用源码分析LinkedList,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

LinkedList简介
public class LinkedList<E>
    extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable{
}
成员变量
/** 当前集合的头节点 **/
transient Node<E> first;

/** 当前集合的尾节点 **/
transient Node<E> last;

/** 当前集合的实际节点数量 **/
transient int size;
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;
    }
}

双向链表的具体节点类,每个节点每个节点又包含三个变量:

构造函数
/** 无参构造一个空的集合 **/
public LinkedList() {
}

/** 根据已有集合构造一个LinkedList **/
public LinkedList(Collection<? extends E> c) {
    this();
    addAll(c);
}
元素添加

集合添加元素方法主要有以下几个方法:

linkFirst(E 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 ++;
}

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

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

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

linkLast(E e)

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

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

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

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

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

add(int index, E element)

/** 添加元素element到当前集合的index位置 **/
public void add(int index, E element) {
    //校验index是否合法
    checkPositionIndex(index);
    if (index == size)
        linkLast(element);
    else
        linkBefore(element, node(index));
}

/** 检查index合法 **/
private void checkPositionIndex(int index) {
    if (index < 0 || index > size) {
        throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
    }
}

/** 在给定元素前插入元素 **/
void linkBefore(E e, Node<E> succ) {
    final Node<E> prev = succ.prev;
    final Node<E> newNode = new Node<>(prev, e, succ);
    succ.prev = newNode;
    if (prev == null) {
        first = newNode;
    } else {
        prev.next = newNode;
    }
    size ++;
    modCount ++;
}

/** 根据index定位元素 **/
private Node<E> node(int index) {
    if (index < size >> 1) {
        Node<E> e = first;
        for (int i = 0; i < index; i++) e = e.next;
        return e;
    } else {
        Node<E> e = last;
        for (int i = size - 1; i > index; i--) e = e.prev;
        return e;
    }
}

addAll(Collection<? extends E> c)

/** 通过下面的addAll(int index, Collection<? extends E> c)方法实现**/
public boolean addAll(Collection<? extends E> c) {
    return addAll(size, c);
}

/** 在当前集合index位置插入集合c中所有元素 **/
public boolean addAll(int index, Collection<? extends E> c) {
    //校验index
    checkPositionIndex(index);
    Object[] a = c.toArray();
    int numNew = a.length;
    if (numNew == 0) return false;
    Node<E> succ, pred;
    if (index == size) {
        succ = null;
        pred = last;
    } else {
        succ = node(index);
        pred = succ.prev;
    }

    for (Object o : a) {
        Node<E> newNode = new Node(pred, o, 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;
}
元素移除

元素移除主要有以下几个方法:

remove(int index)

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

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

private void checkElementIndex(int index) {
    if (index < 0 || index >= size) {
        throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
    }
}

remove(Object o)

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

unlinkFirst(Node<E> 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;
}

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

/** 移除集合头部元素,如果元素为空,抛出NoSuchElementException异常 **/
public E removeFirst() {
    final Node<E> f = first;
    if (f == null) throw new NoSuchElementException();
    return unlinkFirst(f);
}

public E poll() {
    return pollFirst();
}

/** 移除集合头部元素,如果元素为空则返回null **/
public E pollFirst() {
    final Node<E> f = first;
    return f == null ? null : unlinkFirst(f);
}

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

unlinkLast(Node<E> 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;
}

/** 移除集合尾部元素,如果元素为空,抛出NoSuchElementException异常 **/
public E removeLast() {
    final Node<E> l = last;
    if (l == null) throw new NoSuchElementException();
    return unlinkLast(l);
}

/** 移除集合尾部元素,如果元素为空则返回null **/
public E pollLast() {
    final Node<E> l = last;
    return l == null ? null : unlinkLast(l);
}
元素查找

元素查找主要有以下几个方法:

get(int index)

public E get(int index) {
    checkElementIndex(index);
    return node(index).item;
}

getFirst()

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

/** 查找集合头部元素,如果元素为空,抛出NoSuchElementException异常 **/
public E getFirst() {
    final Node<E> e = first;
    if (e == null) throw new NoSuchElementException();
    return e.item;
}

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

/** 查找集合头部元素,如果元素为空就返回null **/
public E peekFirst() {
    final Node<E> node = first;
    return node == null ? null : node.item;
}

getLast()

/** 查找集合尾部元素,如果元素为空,抛出NoSuchElementException异常 **/
public E getLast() {
    final Node<E> e = last;
    if (e == null) throw new NoSuchElementException();
    return e.item;
}

/** 查找集合尾部元素,如果元素为空就返回null **/
public E peekLast() {
    final Node<E> node = last;
    return node == null ? null : node.item;
}
元素更新

元素更新只有一个方法:set(int index, E e)

set(int index, E element)

public E set(int index, E element) {
    checkElementIndex(index);
    //查询index位置的节点
    Node<E> node = node(index);
    E oldValue = node.item;
    //设置为最新元素
    node.item = element
    return oldValue;
}

关于如何用源码分析LinkedList就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. LinkedList源码分析
  2. LinkedList源码分析--jdk1.8

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linkedlist

上一篇:选择Spring Cloud Config作为配置中心的原因是什么

下一篇:看图学NumPy之如何掌握n维数组基础知识点

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》