LinkedList
来源:
- extends:AbstractSequentialList
- implements:
- List
- Deque:deque 即双端队列。是一种具有队列和栈的性质的数据结构。双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。
- Cloneable
- java.io.Serializable
字段:
transient int size = 0;
-
transient Node<E> first
:头节点 -
transient Node<E> last
:尾节点
/**
* Pointer to first node.
* Invariant: (first == null && last == null) ||
* (first.prev == null && first.item != null)
*/
transient Node<E> first;//头节点没有前驱
/**
* Pointer to last node.
* Invariant: (first == null && last == null) ||
* (last.next == null && last.item != null)
*/
transient Node<E> last;//尾节点没有后继
/**
* Node的定义
*/
private static class Node<E> {
E item;//元素
Node<E> next;//后继
Node<E> prev;//前驱啊S
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
方法:
- 构造方法:
/**
* 构造空的list
*/
public LinkedList() {
}
/**
* 按照给定集合的迭代器返回数据的顺序把所有元素添加到LinkedList中
*
* @param c 集合
* @throws NullPointerException
*/
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
- 获取第一个元素
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;
}
- 删除头节点:首节点前驱是空的,然后把原有的item、next置为空,然后让首节点指向原来的第二个节点
first.next
,然后size-1,返回首节点
//实现Deque接口中的方法
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;
}
类似的,删除尾节点的方法也就能理解了
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
private E unlinkLast(Node<E> l) {
...
size--;
modCount++;
return element;
}
- 增加头节点:思路,新建一个Node,前驱为空,后继是原有首节点
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++;
}
类似的新增尾节点也能理解
public void addLast(E e) {
linkLast(e);
}
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++;
}
- 判断函数是否包含某个元素:查找索引,能查到就true,否则false
public boolean contains(Object o) {
return indexOf(o) != -1;
}
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;
}
- 添加结点:设置增添的结点前驱为原尾结点,后继为空;尾结点的next是新节点
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++;
}
对于addAll(Collection c)无非就是多次操作,原理相同
- 删除结点:先寻找这个结点,再把该节点的pre的next指向该节点的next,该节点的next的pre指向该节点的pre.
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;
}
- 清空clear:清除所有的结点是非必须的,但是有助于GC回收释放内存
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++;
}
- 查找索引位置的元素.先判断是否合规:如果索引超出范围,抛出角标越界异常,否则进行从头开始的查找
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
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;
}
}
- 向指定位置添加元素.如果该位置为末端,则直接在末端添加;否则在添加位置+1节点处添加。
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}