LinkedList源码总结
1、LinkedList 底层是基于双向链表实现的
2、LinkedList的每条数据都会被封装成Node<E>
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; } }
3、然后画个图,画出prev指针和next指针,first头指针和last尾指针
4、addFirst() 方法
public void addFirst(E e) { linkFirst(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++; }
目标集合
在头部插入
整理后
5、addLast()方法
public void addLast(E e) { linkLast(e); }
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++; }
目标集合
插入之后
6、add(int index, E element)
public void add(int index, E element) { checkPositionIndex(index); if (index == size) linkLast(element); else linkBefore(element, node(index)); }
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++; }
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; } }
void linkBefore(E e, Node<E> succ) { // assert succ != null; final Node<E> pred = succ.prev; //现在是两个node同时指向前一个节点 final Node<E> newNode = new Node<>(pred, e, succ); //把自己之前的那个线断开,然后指向newNode succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; }
- 首先add(index,e),通过index的坐标,判断是在前半部分还是后半部分,是从头部遍历还是从尾部遍历,找到index对应的Node
- 然后,现在要把index的位置变为e,如图
拉直后
7、removeLast()
public E removeLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return unlinkLast(l); }
private E unlinkLast(Node<E> l) { // assert l == last && l != null; final E element = l.item; final Node<E> prev = l.prev; l.item = null; l.prev = null; // help GC last = prev; if (prev == null) first = null; else prev.next = null; size--; modCount++; return element; }
删除前
过程
结束
8、removeFirst()
public E removeFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); }
private E unlinkFirst(Node<E> f) { // assert f == first && f != null; final E element = f.item; final Node<E> next = f.next; f.item = null; f.next = null; // help GC first = next; if (next == null) last = null; else next.prev = null; size--; modCount++; return element; }
删除前
过程
结束
9、remove(int index)
public E remove(int index) { checkElementIndex(index); return unlink(node(index)); }
private void checkElementIndex(int index) { if (!isElementIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private boolean isElementIndex(int index) { return index >= 0 && index < size; }
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; }
待删除节点
过程
结束
10、getFrist()
定义一个临时变量 f ,等于当前头结点,如果不为null,就返回 item
public E getFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return f.item; }
11、peek():一般当队列用的时候用它
跟 getFirst()方法唯一的区别就是,如果是null也返回
public E peek() { final Node<E> f = first; return (f == null) ? null : f.item; }
12、getLast()
定义一个临时变量 L,等于当前尾节点last,如果不为null,就返回item
public E getLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return l.item; }
13、get(int index)
1. 先检查 index 是否越界
2. 如果没越界根据node方法找到index元素,并返回
3. 注意node方法的一个条件,index < (size >> 1) ,是根据这个条件来判断,是从头开始遍历,还是从尾开始遍历
public E get(int index) { checkElementIndex(index); return node(index).item; }
private void checkElementIndex(int index) { if (!isElementIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private boolean isElementIndex(int index) { return index >= 0 && index < size; }
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; } }
14、总结:LinkedList的 get(index)方法效率比较低,因为需要遍历链表,虽然可通过头节点和尾节点选择从头遍历还是从尾遍历,但总归还是要遍历。但是add()方法要比ArrayList效率高,默认直接插入链表尾部,不用考虑扩容问题。remove(index)和add(index,e)这两个方法虽然需要先遍历到index的位置,但是效率还是比ArrayList高的,不需要数组拷贝。