LinkedList源碼解析
1.成員變量
①size:集合大小
②first:頭結點
③last:尾節點
2.構造器
空構造
帶集合的構造
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
3.重要內部類
private static class Node<E> {
E item;//本身元素
Node<E> next;//前驅,指向該節點的上一個節點,頭結點爲null
Node<E> prev;//後驅,指向該節點的後一個節點,尾節點爲null
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
4主要方法
①添加
//添加頭結點
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數據
first = newNode;
//如果頭結點爲null,那麼這個節點也是尾節點
if (f == null)
last = newNode;
else
//否則將原來頭結點的前驅指想新的頭結點
f.prev = newNode;
size++;
modCount++;
}
//添加尾節點
public void addLast(E e) {
linkLast(e);
}
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++;
}
//在指定位置插入節點數據
public void add(int index, E element) {
//檢查index
checkPositionIndex(index);
if (index == size)
//插入在末尾
linkLast(element);
else
linkBefore(element, node(index));
}
//succ,指定索引位置的節點元素,e要插入的元素數據
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
//取得succ的前一個節點
final Node<E> pred = succ.prev;
//生成新的節點,前驅指向succ的前驅,後驅指向succ
final Node<E> newNode = new Node<>(pred, e, succ);
//succ的前驅指向新節點
succ.prev = newNode;
//檢查succ是否爲頭結點
if (pred == null)
first = newNode;
else
//原來succ的前節點的後驅指向新的節點
pred.next = newNode;
size++;
modCount++;
}
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
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 {
//找到插入索引位置的元素,並將他的前驅賦值給pred
succ = node(index);
pred = succ.prev;
}
for (Object o : a) {
//循環遍歷a數組
@SuppressWarnings("unchecked") E e = (E) o;
//生成新節點,前驅指向pred,後驅爲null
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)
first = newNode;
else
//將pred的後驅指向新節點
pred.next = newNode;
//將新加入的節點作爲新的pred
pred = newNode;
}
//如果索引位置爲null,那麼他的前驅爲尾節點
if (succ == null) {
last = pred;
} else {
//否則,索引位置將succ和他的前驅pred相連
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
/**
* 返回指定索引下的節點
*/
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;
}
}
②刪除(操作過程和添加類似,不做解釋)
public E remove() {
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;
}
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;
if (prev == null)
first = null;
else
prev.next = null;
size--;
modCount++;
return element;
}
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;
}
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
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;
}
③獲取
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
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 boolean contains(Object o) {
return indexOf(o) != -1;
}
//返回對象的下標,沒有返回-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;
}
未完待續