博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LinkedList源码
阅读量:7079 次
发布时间:2019-06-28

本文共 8086 字,大约阅读时间需要 26 分钟。

继承AbstractSequentialList 实现了顺序访问列表
实现List、Deque、Cloneable
List接口定义List集合的操作方法
Deque支持在两端插入和删除元素的线性集合。
Cloneable实现此接口的类可以进行拷贝操作
 
重要说明:
1、定了first头节点(first.pre=null && first.item != null)、last尾节点(last.next=null && last.item != null)
2、clear()清空链表,设置first=last=null,每个节点值设置为null,每个节点next以及每个节点pre设置为null,
3、Node<E> node(int index)获得index位置的节点Node,技巧:先判断index是在size/2之前还是之后,减少一半的查询次数
 
代码翻译:
public class LinkedList
extends AbstractSequentialList
implements List
, Deque
, Cloneable, java.io.Serializable{ 链表长度 transient int size = 0; 定义头节点 transient Node
first; 定义尾节点 transient Node
last; 定义空的链表 public LinkedList() { } 构建包含指定集合c的链表 public LinkedList(Collection
c) { this(); addAll(c); } 将对象e作为链表头,根据对象e构建Node节点,e的pre设置为null,e的next设置为之前头节点f private void linkFirst(E e) { final Node
f = first; final Node
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
l = last; final Node
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
succ) { // assert succ != null; final Node
pred = succ.prev; final Node
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
f) { // assert f == first && f != null; final E element = f.item; final Node
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
l) { // assert l == last && l != null; final E element = l.item; 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; } 将节点x删除掉 E unlink(Node
x) { // assert x != null; final E element = x.item; final Node
next = x.next; final Node
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
f = first; if (f == null) throw new NoSuchElementException(); return f.item; } 获得尾节点的值 public E getLast() { final Node
l = last; if (l == null) throw new NoSuchElementException(); return l.item; } 移除头节点 public E removeFirst() { final Node
f = first; if (f == null) throw new NoSuchElementException(); return unlinkFirst(f); } 移除尾节点 public E removeLast() { final Node
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
x = 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; } 将集合c添加到链表结尾 public boolean addAll(Collection
c) { return addAll(size, c); } 将集合c添加到指定位置 public boolean addAll(int index, Collection
c) { checkPositionIndex(index); Object[] a = c.toArray(); int numNew = a.length; if (numNew == 0) return false; Node
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
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
x = first; x != null; ) { Node
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
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
node(int index) { // assert isElementIndex(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; } } // Search Operations 按照从头到尾的顺序,获得对象o在链表中位置 public int indexOf(Object o) { int index = 0; if (o == null) { for (Node
x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node
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
x = last; x != null; x = x.prev) { index--; if (x.item == null) return index; } } else { for (Node
x = last; x != null; x = x.prev) { index--; if (o.equals(x.item)) return index; } } return -1; } // Queue operations. 查看头节点的值 public E peek() { final Node
f = first; return (f == null) ? null : f.item; } 获得头节点的值,如果f=null,则报错 public E element() { return getFirst(); } 获得头节点并且移除 public E poll() { final Node
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
f = first; return (f == null) ? null : f.item; } 查看尾节点的值 public E peekLast() { final Node
l = last; return (l == null) ? null : l.item; } 返回头节点并且删除 public E pollFirst() { final Node
f = first; return (f == null) ? null : unlinkFirst(f); } 返回尾节点并且删除 public E pollLast() { final Node
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
x = 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; }}

 

转载于:https://www.cnblogs.com/use-D/p/9594958.html

你可能感兴趣的文章
Zabbix使用自带模板监控MySQL
查看>>
tracert和traceroute工作原理方式
查看>>
apache配置虚拟主机及虚拟目录
查看>>
我的友情链接
查看>>
genymotion 极速模拟器
查看>>
RHEL or CentOS配置网络yum源
查看>>
搭建NTP时间服务器
查看>>
oracle 执行计划更改导致数据加工未完成
查看>>
Npp删除选中行的Macro录制方式
查看>>
phpstorm快捷键
查看>>
针对云安全从业者的指南系列九:安全的云通信
查看>>
python写的简单的验证2种类型的email
查看>>
cocos2dx[3.2](8) ——核心类Director/Scene/Layer/Sprite
查看>>
WIN7笔记本做WiFi热点
查看>>
squid
查看>>
图标元素库:FontAwesome
查看>>
ld.conf 动态库详解
查看>>
NAND Flash裸板编程
查看>>
写一个函数返回参数二进制中 1 的个数
查看>>
Go 匿名函数与闭包的使用
查看>>