LinkedList
来源:互联网 发布:网络弊大于利三辩提问 编辑:程序博客网 时间:2024/06/07 06:27
LinkedList
LInkedList是List与Deque的双向链表的实现。相对于ArrayList,LinkedList擅长与添加、删除操作。
常量
transient int size = 0;transient Node<E> first;transient Node<E> last;
- size:表示LinkedList的大小。
- first:指向链表头结点。
- last:指向链表尾结点。
结点结构
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; }}
- Node结构含有键值(item)和分别指向上下结点(next和prev)
构造器
public LinkedList() {}public LinkedList(Collection<? extends E> c) { this(); addAll(c); }
- LinkedList():无参构造器。
- LinkedList(Collection< ? extends E> c):将所有指定集合中的元素插入此列表。
添加元素
public void addFirst(E e) { linkFirst(e);}public void addLast(E e) { linkLast(e); }public boolean add(E e) { linkLast(e); return true;}private void linkFirst(E e) { final Node<E> f = first;//获取头结点即f,并指向first final Node<E> newNode = new Node<>(null, e, f);//创建值为e,pre为null,next为f的结点newNode first = newNode;//将newNode作为链表的头结点 if (f == null) last = newNode; else f.prev = newNode; size++; modCount++; }void linkLast(E e) { final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null);//创建值为e,pre为l,next为null的结点newNode last = newNode;//将newNode作为新的尾结点 if (l == null) first = newNode; else l.next = newNode; size++; modCount++; }public boolean addAll(int index, Collection<? extends E> c) { checkPositionIndex(index);//判断index>=0 &&index<=size 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);//将succ指向index对应的结点 pred = succ.prev;//pred指向succ前结点 }// // 逐个为数组中元素创建结点并插入链表中 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及Fail-Fast 机制的modCount; size += numNew; modCount++; return true; }
删除
public E removeFirst() { final Node<E> f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(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;//将原首结点的next作为现首结点 if (next == null)//若原链表只有一个结点,删除后链表为null last = null; else next.prev = null; size--; modCount++; return element; }public E removeLast() { final Node<E> l = last; if (l == null) throw new NoSuchElementException(); return unlinkLast(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;//将原尾结点的pre作为现尾结点 if (prev == null)//若原链表只有一个结点,删除后链表为null first = null; else prev.next = null; size--; modCount++; return element; }public boolean remove(Object o) {//找到值为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; } 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;//将x结点的上下结点相连 if (prev == null) { first = next; } else { prev.next = next; x.prev = null; } if (next == null) { last = prev; } else { next.prev = prev; x.next = null; }//更新size及fail-fast机制的modCount x.item = null; size--; modCount++; return element; }
- removeFirst():删除头节点,并更新结点间的关系。
- removeLast():删除尾结点,并更新结点间的关系。
- remove(Object o):删除特定元素的结点,找到值为o的结点再删除。
修改
public E set(int index, E element) { checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal;}
- 修改特定索引位置的值,并返回旧值
查找
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 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;}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; }public E get(int index) { checkElementIndex(index); return node(index).item; }
- E getFirst():返回头结点。
- E getLast():返回尾结点。
- indexOf(Object o):返回链表中o元素第一次出现的位置,没有则返回-1。
- lastIndexOf(Object o):返回链表中o元素最后一次出现的位置,没有则返回-1。
- E get(int index) :找到中索引值尾index的元素,并返回。
阅读全文
0 0
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- LinkedList
- gdb调试
- Android EditText 监听回车键
- Java:从键盘获取3个整型数据
- 算法复杂度小记
- 银行排队问题之单队列多窗口加VIP服务(30 分)
- LinkedList
- H264 推流到RTMP服务器
- python MapReduce单词统计
- 脑电波实时数据收集——RDA—数据包定义
- 常见的几种矩阵分解方式
- 混合背包
- 中国国内采购平台大全
- [Unity优化]RaycastTarget勾选过多的烦恼
- 创业社交平台微链获经纬中国、如山资本等2800万元A轮及A+轮投资