LinkedList 特点及增删改查源码解析

特点
  1. 双向链表存储数据
  2. 查询慢、增删除快
1.构造方法
public LinkedList() {
}

public LinkedList(Collection<? extends E> c) {
    this();
	// 调用addAll 详见2.4
    addAll(c);
}
1.1 双向链表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;
    }
}
2.增
public boolean add(E e) {
	// 默认添加到尾部,详见2.1
    linkLast(e);
    return true;
}


public void addFirst(E e) {
	// 添加到头部 , 详见2.2
    linkFirst(e);
}

public void addLast(E e) {
	// 添加到尾部,详见2.1
    linkLast(e);
}


public boolean addAll(Collection<? extends E> c) {
	// 添加集合 详见2.3
    return addAll(size, c);
}
2.1尾部添加linkLast(e)
void linkLast(E e) {
	// last是成员变量记住链表的最后一个node
    final Node<E> l = last;
	// 初始化Node , pre为l , next设置为null(因为是添加到尾部)
    final Node<E> newNode = new Node<>(l, e, null);
    last = newNode;
	// 如果last == null,说明链表是空的
    if (l == null)
		// 让该node指向头部
        first = newNode;
    else
		// 链表不是空的,就让l的next指向新声明的node
        l.next = newNode;
	// 长度++
    size++;
    modCount++;
}
2.2头部添加linkFirst(e)
private void linkFirst(E e) {
    final Node<E> f = first;
    final Node<E> newNode = new Node<>(null, e, f);
    first = newNode;
	// 如果f头部节点为null , 说明链表是空的
    if (f == null)
        last = newNode;
    else
		// 链表不是空 设置当前头部的前一项为新声明的node
        f.prev = newNode;
    size++;
    modCount++;
}
2.3 添加集合
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用于存储临界数据。pred是待插入链表的最前面数据前一项,succ是待插入链表的最后一项的后一项
    Node<E> pred, succ;
	// 如果插在尾部 succ为null
    if (index == size) {
        succ = null;
        pred = last;
    } else {
		// 根据index计算前一项,详见2.4
        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;
}
2.4 找到插入前一项
Node<E> node(int index) {
    // assert isElementIndex(index);
	// 判断下标与size/2比较,<size/2从0开始遍历,>size/2从size-1倒序遍历
    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;
    }
}
3. 删
public E remove() {
	// 默认头部删除
    return removeFirst();
}

public E remove(int index) {
    checkElementIndex(index);
	// 删除节点 详见3.1
    return unlink(node(index));
}

public E removeFirst() {
    final Node<E> f = first;
    if (f == null)
        throw new NoSuchElementException();
	// 删除节点 详见3.1
    return unlinkFirst(f);
}

public E removeLast() {
    final Node<E> l = last;
    if (l == null)
        throw new NoSuchElementException();
	// 删除节点 详见3.1
    return unlinkLast(l);
}

// 遍历链表unlink操作
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;
}
3.1 unlink
// unlink比较逻辑简单
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;
}
4. 改
public E set(int index, E element) {
    checkElementIndex(index);
	// 根据index找到node , node(index)详见2.4
    Node<E> x = node(index);
    E oldVal = x.item;
	// 替换值
    x.item = element;
    return oldVal;
}
5. 查
public E get(int index) {
    checkElementIndex(index);
	// 根据index找到node , node(index)详见2.4
    return node(index).item;
}

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 poll() {
    final Node<E> f = first;
    return (f == null) ? null : unlinkFirst(f);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值