标签:clear 增加 The enc bit ons list list集合 重要
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable { 链表长度 transient int size = 0; 定义头节点 transient Node<E> first; 定义尾节点 transient Node<E> last; 定义空的链表 public LinkedList() { } 构建包含指定集合c的链表 public LinkedList(Collection<? extends E> c) { this(); addAll(c); } 将对象e作为链表头,根据对象e构建Node节点,e的pre设置为null,e的next设置为之前头节点f 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++; } 将对象e作为链表结尾,根据对象e构建Node节点,e的pre设置为之前的结尾l节点,e的next设置为null 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++; } 将对象e设置为succ节点之前,根据对象e构建Node节点,e的pre设置为succ的pre节点,e的next设置为succ节点 void linkBefore(E e, Node<E> succ) { // assert succ != null; final Node<E> pred = succ.prev; final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; } 将头节点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; } 将尾节点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; } 将节点x删除掉 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; } 获得头几点f的值 public E getFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return f.item; } 获得尾节点的值 public E getLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return l.item; } 移除头节点 public E removeFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); } 移除尾节点 public E removeLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return unlinkLast(l); } 添加头节点 public void addFirst(E e) { linkFirst(e); } 添加尾节点 public void addLast(E e) { linkLast(e); } 判断对象o在链表中是否存在 public boolean contains(Object o) { return indexOf(o) != -1; } 返回链表长度 public int size() { return size; } 在尾节点后添加对象e的节点 public boolean add(E e) { linkLast(e); return true; } 移除值为ode节点 public boolean remove(Object o) { if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); return true; } } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } 将集合c添加到链表结尾 public boolean addAll(Collection<? extends E> c) { return addAll(size, c); } 将集合c添加到指定位置 public boolean addAll(int index, Collection<? extends E> c) { checkPositionIndex(index); Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) return false; Node<E> pred, succ; if (index == size) { succ = null; pred = last; } else { succ = node(index); pred = succ.prev; } for (Object o : a) { @SuppressWarnings("unchecked") E e = (E) o; Node<E> newNode = new Node<>(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++; return true; } 清空链表,设置first=last=null,值设置为null,next以及pre设置为null, 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 (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++; } 获得指定位置index的值 public E get(int index) { checkElementIndex(index); return node(index).item; } 将指定位置index的值设置为element public E set(int index, E element) { checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal; } 在指定位置index增加节点e public void add(int index, E element) { checkPositionIndex(index); if (index == size) linkLast(element); else linkBefore(element, node(index)); } 删除指定位置的节点 public E remove(int index) { checkElementIndex(index); return unlink(node(index)); } 判断index位置是否在链表中有值 private boolean isElementIndex(int index) { return index >= 0 && index < size; } 判断index位置是否在链表中 private boolean isPositionIndex(int index) { return index >= 0 && index <= size; } 检查index位置是否在链表中有值 private void checkElementIndex(int index) { if (!isElementIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } 判断index位置是否在链表中 private void checkPositionIndex(int index) { if (!isPositionIndex(index)) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } 获得index位置的节点Node,技巧:先判断index是在size/2之前还是之后,减少一个的查询次数 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; } } // Search Operations 按照从头到尾的顺序,获得对象o在链表中位置 public int indexOf(Object o) { int index = 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; } 按照从尾到头的顺序,获得对象o在链表中位置 public int lastIndexOf(Object o) { int index = 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; } // Queue operations. 查看头节点的值 public E peek() { final Node<E> f = first; return (f == null) ? null : f.item; } 获得头节点的值,如果f=null,则报错 public E element() { return getFirst(); } 获得头节点并且移除 public E poll() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); } 删除头节点 public E remove() { return removeFirst(); } 在尾节点添加对象e的节点 public boolean offer(E e) { return add(e); } 在头节点添加对象为e的头节点 public boolean offerFirst(E e) { addFirst(e); return true; } 在尾节点后添加对象e的节点 public boolean offerLast(E e) { addLast(e); return true; } 查看头节点的值 public E peekFirst() { final Node<E> f = first; return (f == null) ? null : f.item; } 查看尾节点的值 public E peekLast() { final Node<E> l = last; return (l == null) ? null : l.item; } 返回头节点并且删除 public E pollFirst() { final Node<E> f = first; return (f == null) ? null : unlinkFirst(f); } 返回尾节点并且删除 public E pollLast() { final Node<E> l = last; return (l == null) ? null : unlinkLast(l); } 添加在头节点添加对象为e的节点 public void push(E e) { addFirst(e); } 获得头节点并且删除,如果f=null,则报错 public E pop() { return removeFirst(); } 按照从头到尾的顺序,删除对象o在链表中第一次出现的节点 public boolean removeFirstOccurrence(Object o) { return remove(o); } 按照从尾到头的顺序,删除对象o在链表中第一次出现的节点 public boolean removeLastOccurrence(Object o) { if (o == null) { for (Node<E> x = last; x != null; x = x.prev) { if (x.item == null) { unlink(x); return true; } } } else { for (Node<E> x = last; x != null; x = x.prev) { if (o.equals(x.item)) { unlink(x); return true; } } } return false; } }
标签:clear 增加 The enc bit ons list list集合 重要
原文地址:https://www.cnblogs.com/use-D/p/9594958.html