//使用对应参数作为头结点 privatevoidlinkFirst(E e) { final Node<E> f = first; //创建一个新节点,next指向头结点 final Node<E> newNode = newNode<>(null, e, f);//从此处null可以看出底层不是循环链表 first = newNode; if (f == null)//如果头结点为空,即链表长度为0 last = newNode; else//原头结点不为空 f.prev = newNode;//原头结点的pre指向新头结点 size++;//长度+1 modCount++;//修改次数+1 }
//使用对应参数作为尾部结点 voidlinkLast(E e) { //指针l指向原尾节点 final Node<E> l = last; //创建一个新节点,其pre指向原尾节点 final Node<E> newNode = newNode<>(l, e, null); last = newNode;//将新节点设置为链表尾节点 if (l == null)//如果原尾节点为空,即链表长度为0 first = newNode;// else//原尾节点不为空 l.next = newNode;//原尾节点的next指向新尾节点 size++; modCount++; }
//在succ节点前插入新节点 voidlinkBefore(E e, Node<E> succ) { // assert succ != null; final Node<E> pred = succ.prev; final Node<E> newNode = newNode<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode;//目标节点的pre为空,则新插入节点设为链表头结点+ else pred.next = newNode; size++; modCount++; }
//取消链接非空的第一个节点f。 //移除头结点,并将头结点的next设为头结点 private E unlinkFirst(Node<E> f) { // assert f == first && f != null; finalEelement= 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; }
//取消链接非空的尾 节点f。 //移除尾结点,并将尾结点的pre设为尾尾结点 private E unlinkLast(Node<E> l) { // assert l == last && l != null; finalEelement= 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; }
//去除某个特定节点 E unlink(Node<E> x) { // assert x != null; finalEelement= 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; }
if (index < (size >> 1)) { Node<E> x = first; for (inti=0; i < index; i++) x = x.next; return x; } else { Node<E> x = last; for (inti= size - 1; i > index; i--) x = x.prev; return x; } }
//替换下标的节点内容 public E set(int index, E element) { checkElementIndex(index); Node<E> x = node(index); EoldVal= x.item; x.item = element; return oldVal; }
//去除头结点 public E removeFirst() { final Node<E> f = first; if (f == null) thrownewNoSuchElementException(); return unlinkFirst(f); }
//去除尾节点 public E removeLast() { final Node<E> l = last; if (l == null) thrownewNoSuchElementException(); return unlinkLast(l); }
//去除特定元素节点 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 remove(int index) { checkElementIndex(index); return unlink(node(index)); }
publicbooleanaddAll(Collection<? extends E> c) { return addAll(size, c);//index为size时为在链表末尾添加集合 }
//在index元素前插入新集合 publicbooleanaddAll(int index, Collection<? extends E> c) { checkPositionIndex(index);//[0,size]
Object[] a = c.toArray(); intnumNew= a.length; if (numNew == 0) returnfalse;
Node<E> pred, succ; if (index == size) {//index为size时为在链表末尾添加集合 succ = null; pred = last; } else { succ = node(index);//index: [0, size-1] pred = succ.prev; }
for (Object o : a) { @SuppressWarnings("unchecked")Ee= (E) o; Node<E> newNode = newNode<>(pred, e, null);//从此行可以看出集合插入位置 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++; returntrue; }
clear方法:
1 2 3 4 5 6 7 8 9 10 11 12 13
publicvoidclear() { //将对置为空,方便gc回收垃圾 for (Node<E> x = first; x != null; ) { Node<E> next = x.next; x.item = null; x.next = null; x.prev = null; x = next; } first = last = null; size = 0; modCount++; }
publicintindexOf(Object o) { intindex=0; if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; }
publicintlastIndexOf(Object o) { intindex= size; if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { index--; if (x.item == null) return index; } } else { for (Node<E> x = last; x != null; x = x.prev) { index--; if (o.equals(x.item)) return index; } } return -1; }
//出队(从前端),获得第一个元素,不存在会返回null,不会删除元素(节点) public E peekFirst() { final Node<E> f = first; return (f == null) ? null : f.item; } //出队(从后端),获得最后一个元素,不存在会返回null,不会删除元素(节点) public E peekLast() { final Node<E> l = last; return (l == null) ? null : l.item; }
//出队(从前端),获得第一个元素,不存在会返回null,会删除元素(节点) public E pollFirst() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); }
//出队(从后端),获得最后一个元素,不存在会返回null,会删除元素(节点) public E pollLast() { final Node<E> l = last; return (l == null) ? null : unlinkLast(l); }
//入栈,从前面添加 publicvoidpush(E e) { addFirst(e); }
//出栈,返回栈顶元素,从前面移除(会删除) public E pop() { return removeFirst(); }