JavaSE---LinkedList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | /** * 【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(); * } * * } * * */ |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)