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;
        }
    }
    //实现Serilizable接口时,将不需要序列化的属性前添加关键字transient,
	//序列化对象的时候,这个属性就不会序列化到指定的目的地中。
    transient int size = 0;

    //指向首节点
    transient Node<E> first;

    //指向最后一个节点
    transient Node<E> last;

    //构建一个空列表
    public LinkedList() {
    }

    //构建一个包含集合c的列表
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

    //构建一个包含集合c的列表
    public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
    }

    //将集合c中所有元素添加到列表的尾部
    public boolean addAll(Collection<? extends E> c) {
        return addAll(size, c);
    }

    /从指定的位置index开始,将集合c中的元素插入到列表中
    public boolean addAll(int index, Collection<? extends E> c) {
        //判断插入位置的合法性
       if (!isPositionIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        // 将集合转化为数组
        Object[] a = c.toArray();
        // 保存集合大小
        int numNew = a.length;
        // 集合为空,直接返回
        if (numNew == 0)
            return false;

        Node<E> pred, succ; // 定义前驱节点,后继节点
        if (index == size) {// 如果插入位置为链表末尾,则后继为null,前驱为尾结点
            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);
            // 表示在第一个元素之前插入(索引为0的结点)
            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自增1
        modCount++;
        return true;
    }

    Node<E> node(int index) {
        // assert isElementIndex(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;
        }
    }
    add()函数
    //add函数用于向LinkedList中添加一个元素,并且添加到链表尾部。具体添加到尾部的逻辑是由linkLast函数完成的。
    //  将元素添加到末尾
    public boolean add(E e) {
        linkLast(e);
        return true;
    }

    void linkLast(E e) {
        // 保存尾结点,l为final类型,不可更改
        final Node<E> l = last; 
        // 新生成结点的前驱为l,后继为null
        final Node<E> newNode = new Node<>(l, e, null);
        // 重新赋值尾结点
        last = newNode;
        // 尾结点为空
        if (l == null)
            first = newNode;// 赋值头结点
        else
            l.next = newNode;// 尾结点的后继为新生成的结点
        //将数组的size自增1
        size++;
        //将modCount自增1
        modCount++;
    }
    remove()函数


    public E remove(int index) {
    //检测数组是否越界
        if (!isElementIndex(index))
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        return unlink(node(index));
    }
    

    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;
    }     
   clear() 函数

   public void clear() {

        for (Node<E> x = first; x != null; ) {
            //循环遍历迭代,将所有的节点设置为null
            Node<E> next = x.next;
            x.item = null;
            x.next = null;
            x.prev = null;
            x = next;
        }
        //将第一个与最后一个节点设置null
        first = last = null;
        //将size设置为0
        size = 0;
        //将modCount自增1
        modCount++;
    }

猜你喜欢

转载自blog.csdn.net/zxk082829/article/details/89741673