List之LinkedList

1、List与LinkedList之间的关系

    LinkedList实现了List接口

2、LinkedList继承实现关系

    LinkedList继承自AbstractSequentialList类,实现了List、Deque、Cloneable、Serializable接口

3、关系图

    实线表示继承、虚线表示实现。基于JDK1.8

4、LinkedList重要属性

    // 存放长度
    transient int size = 0;

    /**
     * 存放首节点
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first;

    /**
     * 存放最后一个节点
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last;

5、new LinkedList<>()的流程

    /**
     * 空实现
     */
    public LinkedList() {
    }

6、add()方法的流程

    public boolean add(E e) {
        linkLast(e);  // 添加元素到最后的节点
        return true;
    }
    /**
     * 将e添加到列表的最后
     */
    void linkLast(E e) {
        // 将最后节点赋值给l
        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++;
    }

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

总的来说LinkedList添加的时候只是判断是否是首次插入,如果是,则将LinkedList的首节点指向新节点,并且将last节点指向新节点,如果不是,那么将上次插入节点对象的next属性指向新节点,并且将最后的节点对象指向新节点

7、remove(e)方法流程

    public boolean remove(Object o) {
    // 判断元素是否为空
    if (o == null) { // 如果是空
            // 当首节点不为空的时候开始循环查找节点对象为空的节点
            for (Node<E> x = first; x != null; x = x.next) {
                // 如果节点元素等于null则执行删除该元素
                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) {
        // 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;
    }









 







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值