publicbooleanaddAll(Collection<? extends E> c){ return addAll(size, c); } publicbooleanaddAll(int index, Collection<? extends E> c){ //下标越界检查 checkPositionIndex(index); //将传入的集合转为数组类型 Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) returnfalse;
//succ为index当前的节点,pred是当前节点的上一个节点 Node<E> pred, succ; //如果从尾部插入 if (index == size) { succ = null; pred = last; } else { //从链表头部或中间插入 succ = node(index); pred = succ.prev; }
//将集合a中的元素依次插入链表 for (Object o : a) { @SuppressWarnings("unchecked") E e = (E) o; Node<E> newNode = new Node<>(pred, e, null); //pred == null是从头部插入 if (pred == null) first = newNode; else //将新节点设为pred的下一个节点 pred.next = newNode; pred = newNode; }
//从尾部插入的话,需要处理last指针 if (succ == null) { last = pred; } else { pred.next = succ; succ.prev = pred; }
Node<E> node(int index) { //如果 index < size/2 则从0开始查找指定下标的节点 if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { //如果index >= size/2 则从size-1向前查找指定下标的节点 Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
linkFirst()
在头部插入节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14
privatevoidlinkFirst(E e){ final Node<E> f = first; //头结点作为新建节点的后一个节点 final Node<E> newNode = new Node<>(null, e, f); //当前节点指向first指针 first = newNode; if (f == null) last = newNode; else //原来的头节点的上一个节点指向新建节点 f.prev = newNode; size++; modCount++; }
linkLast()
在尾部插入节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14
voidlinkLast(E e){ final Node<E> l = last; //尾节点作为新建节点的上一个节点 final Node<E> newNode = new Node<>(l, e, null); //当前节点指向last指针 last = newNode; if (l == null) first = newNode; else //原来的尾节点的下一个节点指向新节点 l.next = newNode; size++; modCount++; }
linkBefore()
在指定位置前插入节点
1 2 3 4 5 6 7 8 9 10 11 12 13
voidlinkBefore(E e, Node<E> succ){ //假设succ不为null,保存succ节点的上一个节点为pred final Node<E> pred = succ.prev; //新建节点,将pred和succ分别作为新节点的上一个和下一个节点 final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; }
unlinkFirst()
删除头节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
private E unlinkFirst(Node<E> f){ //假设f为头节点且f不为null final E element = f.item; final Node<E> next = f.next; f.item = null; f.next = null; // help GC //将first指针指向f的下一个节点 first = next; //处理链表只有一个头节点的情况 if (next == null) last = null; else next.prev = null; size--; modCount++; return element; }
unlinkLast()
删除尾节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
private E unlinkLast(Node<E> l){ //假设l为尾节点且尾节点不为null final E element = l.item; final Node<E> prev = l.prev; l.item = null; l.prev = null; // help GC //将last指针指向尾节点的上一个节点 last = prev; //处理链表只有一个尾节点的情况 if (prev == null) first = null; else prev.next = null; size--; modCount++; return element; }
public E remove(int index){ //下标越界检查 checkElementIndex(index); return unlink(node(index)); } publicbooleanremove(Object o){ if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); returntrue; } } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); returntrue; } } } returnfalse; }
public E peek(){ final Node<E> f = first; return (f == null) ? null : f.item; }
element()
获取头节点但不移除,若头节点为null的话就抛出异常,时间复杂度O(1)
1 2 3 4 5 6 7 8 9
public E element(){ return getFirst(); } public E getFirst(){ final Node<E> f = first; if (f == null) thrownew NoSuchElementException(); return f.item; }
poll()
移除头节点,头节点为null就返回null,时间复杂度O(1)
1 2 3 4
public E poll(){ final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); }