LinkedList源码分析

LinkedList源码分析

大家可以先看一下这篇java简单模拟双向链表 - CoderDreams - 博客园 (cnblogs.com)

结论:

  1. 维护的是一个双向链表

无参构造器

public LinkedList() {
}

无参实例化后,维护的是一个双向链表(此时:size=0;first=null;last=null,modCount=0)


add()

public boolean add(E e) {
    // 将参数传入用于双向链表的指向
    linkLast(e);
    //  百分百返回true
    return true;
}

// Links e as last element.
// 将新元素作为最后一个元素加入维护的双向链表
void linkLast(E e) {
    	// last赋给l
        final Node<E> l = last;
    	
    	// 将newNode指向一个新实例化的一个Node<>
        final Node<E> newNode = new Node<>(l, e, null);
    	/*
    		Node<>类为
    		private static class Node<E> {
        		E item;
        		Node<E> next;
        		Node<E> prev;
        		// 将last作为新实例化的pre传入
        		// 并将null作为last传入
        		Node(Node<E> prev, E element, Node<E> next) {
            		this.item = element;
            		this.next = next;
            		this.prev = prev;
        		}
    		}
    	*/
    	// 将last指向新实例化的newNode
    	// 此时l指向之前的last,没变
        last = newNode;
    	// 如果之前的last为空
    	// 也就是说e是第一个元素时
        if (l == null)
            // 将first也指向它
            first = newNode;
        else
            // 如果不是就将之前的last的next指向newNode
            l.next = newNode;
    	// size++
        size++;
    	// 修改次数++
        modCount++;
    }

remove()

无参时删除的是第一个元素

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

public E removeFirst() {
    	// 将first指向赋给f
        final Node<E> f = first;
    	// 如果first为null,也就是头节点没有元素
        if (f == null)
            // 抛出异常
            throw new NoSuchElementException();
    	// 如果有头节点元素
        return unlinkFirst(f);
    }

// Unlinks non-null first node f.
// 解除非空的第一个节点f的链接。
private E unlinkFirst(Node<E> f) {
        // assert f == first && f != null;
    	// 要求f为first 并且 f不为null(从removeFirst()进来就是满足的)
    	// 用element指向f的item
        final E element = f.item;
    	// 用next指向f的next
        final Node<E> next = f.next;
    	// 把null赋给f的item和next
    	// 将f断开变成垃圾
        f.item = null;
        f.next = null; // help GC	帮助GC认为这是个垃圾,把它回收干掉
    	// 用first指向上面指向原先f.next的next
        first = next;
    	// 如果next是null
    	// 如果f后面没有值了
        if (next == null)
            // 那就将last指向null
            last = null;
        else
            // 否则将null赋给next的prev
            next.prev = null;
    	// size--
        size--;
    	// 修改次数++
        modCount++;# LinkedList源码分析

> 大家可以先看一下这篇[java简单模拟双向链表 - CoderDreams - 博客园 (cnblogs.com)](https://www.cnblogs.com/coderDreams/p/15927238.html)

## 结论:

1. 维护的是一个双向链表

### 无参构造器

```java
public LinkedList() {
}

无参实例化后,维护的是一个双向链表(此时:size=0;first=null;last=null,modCount=0)


add()

public boolean add(E e) {
    // 将参数传入用于双向链表的指向
    linkLast(e);
    //  百分百返回true
    return true;
}

// Links e as last element.
// 将新元素作为最后一个元素加入维护的双向链表
void linkLast(E e) {
    	// last赋给l
        final Node<E> l = last;
    	
    	// 将newNode指向一个新实例化的一个Node<>
        final Node<E> newNode = new Node<>(l, e, null);
    	/*
    		Node<>类为
    		private static class Node<E> {
        		E item;
        		Node<E> next;
        		Node<E> prev;
        		// 将last作为新实例化的pre传入
        		// 并将null作为last传入
        		Node(Node<E> prev, E element, Node<E> next) {
            		this.item = element;
            		this.next = next;
            		this.prev = prev;
        		}
    		}
    	*/
    	// 将last指向新实例化的newNode
    	// 此时l指向之前的last,没变
        last = newNode;
    	// 如果之前的last为空
    	// 也就是说e是第一个元素时
        if (l == null)
            // 将first也指向它
            first = newNode;
        else
            // 如果不是就将之前的last的next指向newNode
            l.next = newNode;
    	// size++
        size++;
    	// 修改次数++
        modCount++;
    }

remove()

无参时删除的是第一个元素

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

public E removeFirst() {
    	// 将first指向赋给f
        final Node<E> f = first;
    	// 如果first为null,也就是头节点没有元素
        if (f == null)
            // 抛出异常
            throw new NoSuchElementException();
    	// 如果有头节点元素
        return unlinkFirst(f);
    }

// Unlinks non-null first node f.
// 解除非空的第一个节点f的链接。
private E unlinkFirst(Node<E> f) {
        // assert f == first && f != null;
    	// 要求f为first 并且 f不为null(从removeFirst()进来就是满足的)
    	// 用element指向f的item
        final E element = f.item;
    	// 用next指向f的next
        final Node<E> next = f.next;
    	// 把null赋给f的item和next
    	// 将f断开变成垃圾
        f.item = null;
        f.next = null; // help GC	帮助GC认为这是个垃圾,把它回收干掉
    	// 用first指向上面指向原先f.next的next
        first = next;
    	// 如果next是null
    	// 如果f后面没有值了
        if (next == null)
            // 那就将last指向null
            last = null;
        else
            // 否则将null赋给next的prev
            next.prev = null;
    	// size--
        size--;
    	// 修改次数++
        modCount++;
    	// 将element返回
        return element;
    }

// 最后返回到remove()
public E remove() {
    // remove()的返回值为删除的元素的item
    return removeFirst();
}
	// 将element返回
    return element;
}

// 最后返回到remove()
public E remove() {
// remove()的返回值为删除的元素的item
return removeFirst();
}

posted @ 2022-02-23 15:15  CoderCatIce  阅读(34)  评论(0编辑  收藏  举报