1.构造方法
LinkedList中有两种构造方法:
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
第一种构造一个空的方法、第二种通过一个实现Collection接口的对象来初始化数据(可以是另一个LinkList以及ArrayLlist等)实现方式是通过addAll这个方法:
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;
}
在addAll()方法中我们是通过链表这个数据结构进行插入操作。
链表节点的定义如下:
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;
}
}
我们可以看到链表节点中有前驱prev、以及后继next、数据域item。
2.公共方法
1.getFirst()
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
LinkeList是基于链表数据结构实现的,此方法将链表的头节点返回。
2.getLast()
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
与getFirst()类似,将尾节点返回。
以上两个访问数据的时间效率都是O(1)。
3.removeFirst()
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
此方法可以删除掉头节点,其实核心是调用的
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;
}
这个方法中首先将头节点的下一个节点保存起来,再将头结点的信息以及后继置空(让这个头结点提早被垃圾回收机制探测到),最后将链表大小减1,返回头结点数据。
4.removeLast()
删除尾节点,与上一个方法操作类似不做详细介绍。
5.addFirst(E 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++;
}
就是一个简单的头部插入。
6.addLast(E e)
在尾部插入数据,类似于上一个头部插入操作
我们可以看到在头部和尾部插入数据的效率均势哦O(1)。
7.contains(Object o)
是否包含这个数据,其核心代码是调用的
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;
}
我们可以看到链表是通过一次线性查找来查找数据,所以时间效率也是线性的。
8.size()
返回链表的大小,在类中维护了一个int型的变量来表示大小,直接返回即可
9.add()
向链表尾部插入一个数据,与addLast一样都是调用的
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++;
}
简单的链表数据插入
10.remove()
public boolean remove(Object 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;
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;
}
断开这个节点,还是将这个节点的前驱后继以及数据都置空,以便gc
可以看到删除操作的时间复杂度也是O(n)
11.clear()
public void clear() {
for (Node<E> x = first; x != null; ) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
删除整个链表,实现方式是将所有节点数据置空,再将首尾节点置空,最后将大小置为0。