本文共 9838 字,大约阅读时间需要 32 分钟。
/*LinkedList size*/transient int size = 0; /** * Pointer to first node.当前链头 * Invariant: (first == null && last == null) || * (first.prev == null && first.item != null) */ transient Nodefirst; /** * Pointer to last node.当前链尾 * Invariant: (first == null && last == null) || * (last.next == null && last.item != null) */ transient Node last;
public LinkedList() { }//Collection集合转成LinkedList public LinkedList(Collection c) { this(); addAll(c); }
public boolean addAll(Collection c) { return addAll(size, c); } }
public boolean addAll(int index, Collection c){ checkPositionIndex(index);//检查index是否超过size Object[] a = c.toArray(); int numNew = a.length; //Collection本身为空集合则直接返回false if (numNew == 0) return false; //定义上个节点,成功节点 Nodepred, succ; //如果从链尾添加则链尾Last为pred,succ为null if (index == size) { succ = null; pred = last; } else { //否则获取index所在Node节点,指向succ succ = node(index); //获取succ的上个节点指向pred pred = succ.prev; } //迭代Collection的数组 for (Object o : a) { @SuppressWarnings("unchecked") E e = (E) o; //创建新节点,并完成(pred<-e)左链关系 Node newNode = new Node<>(pred, e, null); //如果上个节点为空则newNode指向链头 if (pred == null) first = newNode; //否则完成(pred<=>e)双向链表 else pred.next = newNode; //重置pred,令新节点newNode指向pred,然后继续迭代 pred = newNode; } //链尾添加则将迭代完的pred指向last if (succ == null) { last = pred; } else { //否则建立双向链表pred<=>succ pred.next = succ; succ.prev = pred; } //更新size size += numNew; modCount++; return true; }
private void linkFirst(E e) { //备份原首元素至f final Nodef = first; //构造newNode,并指向为当前链表头first final Node newNode = new Node<>(null, e, f); first = newNode; //如果原首元素为空,则newNode也是链表尾 if (f == null) last = newNode; else //否则建立双向链关系 f<=>newNode f.prev = newNode; size++; modCount++; }
void linkLast(E e) { //备份尾元素 final Nodel = last; //新建节点并完成单相关系l <= newNode final Node newNode = new Node<>(l, e, null); //newNode成为新的链表尾 last = newNode; //原链表尾为空,则newNode将成为新链表头 if (l == null) first = newNode; else //否则建立双向链表关系 l <=> newNode l.next = newNode; size++; modCount++; }
void linkBefore(E e, Nodesucc) { // assert succ != null;succ必须非空,否则空指针 //备份succ的上个节点prev final Node pred = succ.prev; //建立新节点,并完成单向链关系pred<-newNode->succ final Node newNode = new Node<>(pred, e, succ); //pred<-newNode<=>succ succ.prev = newNode; //pred为空则newNode成为链表头 if (pred == null) first = newNode; else //否则建立双向关系pred<=>newNode<=>succ pred.next = newNode; size++; modCount++; }
private E unlinkFirst(Nodef) { // assert f == first && f != null; //f必须为链表头并且非空 //备份原链头,并用于返回 final E element = f.item; //获取原链次节点 final Node next = f.next; f.item = null; f.next = null; // help GC //原表链次节点成为当前链头first first = next; //如果原表链次节点为空,则尾也为空 if (next == null) last = null; else //否则至链头的上节点为空 next.prev = null; size--; modCount++; return element; }
private E unlinkLast(Nodel) { // assert l == last && l != null;//参数l必须为链尾并且非空 //备份原链尾,并用于返回 final E element = l.item; //原链尾上节点作为最新链尾last final Node 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(Nodex) { // assert x != null;//目标节点必须不为空 final E element = x.item; final Node next = x.next; final Node prev = x.prev; //目标节点的上节点prev为空,即目标节点为链表头,所以next就成为链头 if (prev == null) { first = next; } else { //否则为建立单向关系prev->next prev.next = next; x.prev = null;//拆除原节点prev关系 } //目标节点的下节点next为空,即目标节点为链表尾,所以prev就成为链尾 if (next == null) { last = prev; } else { //否则为建立双向关系prev<=>next next.prev = prev; x.next = null;//拆除原节点next关系 } //除原节点item置空,help gc x.item = null; size--; modCount++; return element; }
public E getFirst() { final Nodef = first; if (f == null) throw new NoSuchElementException(); return f.item; }
public E getLast() { final Nodel = last; if (l == null) throw new NoSuchElementException(); return l.item; }
public E removeFirst() { final Nodef = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); }
public E removeLast() { final Nodel = last; if (l == null) throw new NoSuchElementException(); return unlinkLast(l); }
public boolean contains(Object o) { return indexOf(o) != -1; }
public int indexOf(Object o) { int index = 0; //o为空则迭代判断出首空元素,并返回下标 if (o == null) { for (Nodex = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { o非空则迭代判断equals一致对象,并返回下标 for (Node x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; }
public int lastIndexOf(Object o) { int index = size; if (o == null) { //反向迭代 //o为空则迭代判断出最后空元素,并返回下 for (Nodex = last; x != null; x = x.prev) { index--; if (x.item == null) return index; } } else { //o非空则迭代判断equals最后一致对象,并返回下标 for (Node x = last; x != null; x = x.prev) { index--; if (o.equals(x.item)) return index; } } return -1; }
public boolean removeFirstOccurrence(Object o) { return remove(o); }
public boolean remove(Object o) { if (o == null) { //迭代出首空元素并完成拆链 for (Nodex = first; x != null; x = x.next) { if (x.item == null) { unlink(x); return true; } } } else { //迭代出首元素并完成拆链 for (Node x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; }
public void clear() { // Clearing all of the links between nodes is "unnecessary", but: // - helps a generational GC if the discarded nodes inhabit // more than one generation // - is sure to free memory even if there is a reachable Iterator //迭代完成拆链 for (Nodex = first; x != null; ) { Node next = x.next; x.item = null; x.next = null; x.prev = null; x = next; } first = last = null; size = 0; modCount++; }
public E get(int index) { checkElementIndex(index);//判断下标不能超过size return node(index).item; }
Nodenode(int index) { // assert isElementIndex(index); 判断下标不能超过size //判断index实在链前半还是链后半,选择正向还是反向迭代 if (index < (size >> 1)) { Node x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
//都是反向迭代,分null和equals public boolean removeLastOccurrence(Object o) { if (o == null) { for (Nodex = last; x != null; x = x.prev) { if (x.item == null) { unlink(x); return true; } } } else { for (Node x = last; x != null; x = x.prev) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; }
转载地址:http://dpega.baihongyu.com/