JavaSE---LinkedList
/** * 【java.util.Queue】 * * The {@link #remove()} and {@link #poll()} methods remove and return the head of the queue. * Exactly which element is removed from the queue is a function of the queue's ordering policy, which differs from implementation to implementation. * The {@code remove()} and {@code poll()} methods differ only in their behavior when the queue is empty: the {@code remove()} method throws an exception, while the {@code poll()} method returns {@code null}. * remove、poll 方法 移除并返回 头元素; * 从队列中 删除哪个元素 是队列排序策略的一个函数,这在不同的实现中是不同的。 * remove 和 poll 在空队列中行为不同:remove 抛异常、poll 返回null; * * The {@link #element()} and {@link #peek()} methods return, but do not remove, the head of the queue. * element 和 peek 方法 仅返回(不移除)头元素; * * {@code Queue} implementations generally do not allow insertion of {@code null} elements. * Queue 实现 不允许插入null元素值; * * public interface Queue<E> extends Collection<E> { * * } * * * 【java.util.Deque】 * A linear collection that supports element insertion and removal at both ends. * The name deque is short for "double ended queue". * 线性集合:支持 双端 插入、移除; * double ended queue 的缩写; * * This interface extends the {@link Queue} interface. * When a deque is used as a queue, FIFO (First-In-First-Out) behavior results. * Deques can also be used as LIFO (Last-In-First-Out) stacks. * * * * */ /** * 【java.util.LinkedList】 * Doubly-linked list implementation of the {@code List} and {@code Deque} interfaces. * Implements all optional list operations, and permits all elements (including {@code null}). * 实现了 List、Deque 接口的 双向链表; * LinkedList 允许 null元素值; * * public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable{ * * 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; * } * } * * transient int size = 0; * transient Node<E> first; * transient Node<E> last; * * * // ***时间复杂度分析 * * // >>>add 追加到tail node * O(1); * public boolean add(E e) { * linkLast(e); * return true; * } * * 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++; * } * * public void addFirst(E e) { * linkFirst(e); * } * * public void addLast(E e) { * linkLast(e); * } * * public void add(int index, E element) { insert * if (index == size) * linkLast(element); // tail 插入, O(1) * else * linkBefore(element, node(index)); // 中间插入, O(n/2) * } * * public E set(int index, E element) { replace * Node<E> x = node(index); * E oldVal = x.item; * x.item = element; * return oldVal; * } * * // >>>get * O(n/2) * public E get(int index) { * checkElementIndex(index); * return node(index).item; * } * * Node<E> node(int 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; * } * } * * // ***remove * public boolean remove(Object o) { * if (o == null) { * for (Node<E> x = first; x != null; x = x.next) { // head O(1) * if (x.item == null) { // tail O(n) * 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; * } * * public E remove() { // O(1) * return removeFirst(); * } * * // ***indexOf * public int indexOf(Object o) { * int index = 0; * if (o == null) { * for (Node<E> x = first; x != null; x = x.next) { // head O(1) * if (x.item == null) // tail O(n) * return index; * index++; * } * } else { * for (Node<E> x = first; x != null; x = x.next) { * if (o.equals(x.item)) * return index; * index++; * } * } * return -1; * } * * // ***peek * public E peek() { // Retrieves, but does not remove, the head (first element) of this list. * final Node<E> f = first; * return (f == null) ? null : f.item; * } * * // ***poll * public E poll() { // Retrieves and removes the head (first element) of this list. * final Node<E> f = first; * return (f == null) ? null : unlinkFirst(f); * } * * // ***pop * public E pop() { * return removeFirst(); * } * * } * * */