简介
LinkedList是一个继承于AbstractSequentialList并实现List接口、Deque接口、Cloneable接口的双向链表 。
- 可以作为栈、队列和双端队列来使用。
- 可以包含重复元素、可以包含null。
- 未实现同步,因此线程不安全。
- 插入删除效率高,查找效率低(基于索引查找有优化,下面会讲到)。
数据结构
源码
Node类
相当简单,存储任意类型的值及指向上一节点和下一节点的引用。
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;
}
}
构造方法
支持集合框架的其他实现转化为LinkedList存储。
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
//转化为LinkedList;index 插入指定位置。
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;
}
新增节点
LinkedList的新增节点方法很多,但是大同小异。如add()方法添加节点至链表尾部、push()方法添加节点至链表头部、offer()方法加节点至链表尾部等等。
add(E e)方法
//add方法添加节点至链表尾部
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++;
}
push(E e)方法
//push方法添加节点至链表头部
public void push(E e) {
addFirst(e);
}
public void addFirst(E e) {
linkFirst(e);
}
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++;
}
offer(E e)方法
public boolean offer(E e) {
return add(e);
}
修改节点
public E set(int index, E element) {
checkElementIndex(index);
//重点看node(int index)方法
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
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;
}
}
查找节点
LinkedList的查找节点方法很多,一样是大同小异。如get()方法查找指定索引元素、peek()方法查找头节点元素、poll()方法则返回头节点元素的值并删除该节点等等。
get(int index)方法
// 查找指定位置元素
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
peek()方法
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
poll()方法
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
删除节点
LinkedList的删除节点方法很多,一样是大同小异。如remove()方法删除头结点、pop()方法删除头结点等等。
remove()方法
remove()有多个重载方法。如remove(Object o)、remove(int index)等。
public E remove() {
return removeFirst();
}
public E pop() {
return removeFirst();
}
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;
if (next == null)
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}
本文由 Administrator 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站部分文章采集自互联网,因某些原因未注明出处,如有侵权,请留言告知。