LinkedList介紹
老規矩我們先看下LinkedList的繼承關係圖:
②.LinkedList基於雙向連結串列,實現了所有List操作並允許所有元素包括null值,它可以被當作雙端佇列.
③.LinkedList順序訪問非常高效,而隨機訪問效率很低.
④.LinkedList執行緒不安全,可以用Collections.synchronizedList使其執行緒安全.
原始碼分析
LinkedList欄位
/**
* 序列號
*/
private static final long serialVersionUID = 876323262645176354L;
/**
* LinkedList長度,不可序列化
*/
transient int size = 0;
/**
* 首結點
*/
transient Node first;
/**
* 尾結點
*/
transient Node last;
複製程式碼
結點類:
/**
* 結點類
*/
private static class Node {
// 當前結點所包含的值
E item;
// 後繼結點
Node next;
// 前驅結點
Node prev;
//結點構造方法
Node(Node prev, E element, Node next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}
複製程式碼
>
構造方法
/**
* 無參構造
*/
public LinkedList() {
}
/**
* 引數Collection的構造方法
*/
public LinkedList(Collection c) {
// 呼叫無參建構函式
this();
// 新增集合中所有的元素
addAll(c);
}
複製程式碼
CRUD
Create
LinkedList新增(public)有如下幾個方法:
public boolean add(E e);
public boolean offer(E e);
public void addLast(E e);
public boolean offerLast(E e);
/**
* 末尾插入元素
*/
public boolean add(E e) {
linkLast(e);
return true;
}
public boolean offer(E e) {
// 尾插元素
return add(e);
}
/**
* 尾插元素
*/
public void addLast(E e) {
linkLast(e);
}
/**
* 尾插元素
*/
public boolean offerLast(E e) {
addLast(e);
return true;
}
/**
* 尾插元素
*/
void linkLast(E e) {
//獲取當前尾結點
final Node l = last;
//定義新結點,其前驅結點為尾結點,值為e,後繼結點為null
final Node newNode = new Node<>(l, e, null);
//將剛定義的新節點設為尾結點
last = newNode;
//若原尾結點為null,即原連結串列為null,則連結串列首結點也為newNode
if (l == null)
first = newNode;
//若不是原尾結點的後繼設為newNode
else
l.next = newNode;
長度+1
size++;
改變次數+1
modCount++;
}
複製程式碼
public void add(int index, E element);
public boolean addAll(Collection<? extends E> c);
public boolean addAll(int index, Collection<? extends E> c);
/**
* 指定位置插入元素
*/
public void add(int index, E element) {
// 判斷索引是否越界
checkPositionIndex(index);
// 若指定位置在尾部,則尾插元素;若不在調通用方法指定位置插入
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
/**
* 尾插集合所有元素
*/
public boolean addAll(Collection c) {
return addAll(size, c);
}
/**
* 指定位置插入集合所有元素
*/
public boolean addAll(int index, Collection c) {
//判斷索引是否越界
checkPositionIndex(index);
//將集合轉為陣列
Object[] a = c.toArray();
//獲取陣列長度
int numNew = a.length;
//若陣列長度為0,即沒有要插入的元素返回false
if (numNew == 0)
return false;
//succ為原index位置上結點,pred為succ前驅結點
Node pred, succ;
//若是尾部,succ為null,pred為尾結點
if (index == size) {
succ = null;
pred = last;
//若不是succ為index位置結點,pred為其前驅結點
} else {
succ = node(index);
pred = succ.prev;
}
//for迴圈遍歷集合
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
//定義一個新結點,其前驅結點為pred,結點值為e,後繼結點為null
Node newNode = new Node<>(pred, e, null);
//若前驅結點為null,則將newNode設為首結點
if (pred == null)
first = newNode;
else
//若存在前驅結點,將其後繼賦為newNode
pred.next = newNode;
類似於迭代器的next,將newNode指向下一個需插入結點位置的前驅
pred = newNode;
}
//若是尾插,則最後新增元素為尾結點
if (succ == null) {
last = pred;
} else {
//若不是,最後新增的結點後繼指向原index位置上的succ
pred.next = succ;
//succ的前驅指向最後新增的結點
succ.prev = pred;
}
//長度+numNew
size += numNew;
modCount++;
return true;
}
private void checkPositionIndex(int index) {
//若index不在[0,size]區間內則拋越界異常
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
/**
* 獲取指定位置結點
*/
Node node(int index) {
// 若指定索引小於LinkedList長度一半,則從首結點開始遍歷;若不是從尾結點開始遍歷
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;
}
}
/**
* 中間插入
*/
void linkBefore(E e, Node succ) {
// 獲取原索引上元素前驅結點pred
final Node pred = succ.prev;
// 定義新結點,其前驅結點為pred,結點值為e,後繼為succ
final Node newNode = new Node<>(pred, e, succ);
// 將succ的前驅結點設為newNode
succ.prev = newNode;
// pred為空,即succ為原首結點,那麼將新定義的newNode設為新首結點
if (pred == null)
first = newNode;
else
//否則pred後繼結點設為newNode
pred.next = newNode;
//長度+1
size++;
//修改次數+1
modCount++;
}
複製程式碼
public boolean offerFirst(E e);
public void addFirst(E e);
public void push(E e);
/**
* 頭插指定元素
*/
public void addFirst(E e) {
linkFirst(e);
}
public void push(E e) {
addFirst(e);
}
/**
* 頭插指定元素
*/
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
/**
* 頭插
*/
private void linkFirst(E e) {
//獲取當前首結點
final Node f = first;
//定義新結點,前驅為null,結點值為e,後驅為f
final Node newNode = new Node<>(null, e, f);
//將newNode設為首結點
first = newNode;
//若首結點為null,尾結點設為newNode,若不是原首結點前驅設為newNode
if (f == null)
last = newNode;
else
f.prev = newNode;
長度+1
size++;
modCount++;
}
複製程式碼
Retrieve
LinkedList查詢(public)有如下幾個方法:
public boolean contains(Object o);
public int size();
public E element();
public E get(int index);
public E getFirst();
public E getLast();
public int indexOf(Object o);
public int lastIndexOf(Object o);
public int size();
public E peek();
public E peekFirst();
public E peekLast();
/**
* 查詢是否包含此元素
*/
public boolean contains(Object o) {
return indexOf(o) != -1;
}
/**
* 迴圈從頭遍歷找出指定元素索引,-1表示不存在該元素
*/
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;
}
/**
* 查詢LinkedList長度
*/
public int size() {
return size;
}
/**
* 獲取首結點值,若連結串列為空會拋異常
*/
public E element() {
return getFirst();
}
/**
* 獲取首結點值,若連結串列為空會拋異常
*/
public E getFirst() {
final Node f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
/**
* 獲取首結點值,若無首結點返回null
*/
public E peek() {
final Node f = first;
return (f == null) ? null : f.item;
}
/**
* 獲取首結點值,若無首結點返回null
*/
public E peekFirst() {
final Node f = first;
return (f == null) ? null : f.item;
}
/**
* 獲取指定索引元素
*/
public E get(int index) {
//校驗是否越界
checkElementIndex(index);
return node(index).item;
}
/**
* 獲取尾結點,,若連結串列為空會拋異常
*/
public E getLast() {
final Node l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
/**
* 獲取尾結點值,若連結串列為空返回null
*/
public E peekLast() {
final Node l = last;
return (l == null) ? null : l.item;
}
/**
* 迴圈從尾遍歷找出指定元素索引,-1表示不存在該元素
*/
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;
}
複製程式碼
Update
public E set(int index, E element);
/**
* 修改指定位置結點值,返回被替換結點值
*/
public E set(int index, E element) {
//校驗index是否越界
checkElementIndex(index);
//獲取當前index位置上結點
Node x = node(index);
//獲取此結點值
E oldVal = x.item;
//修改結點值
x.item = element;
//返回被替換結點值
return oldVal;
}
複製程式碼
Delete
public E remove();
public E remove(int index);
public boolean remove(Object o);
public E removeFirst();
public boolean removeFirstOccurrence(Object o);
public E removeLast();
public boolean removeLastOccurrence(Object o);
public void clear();
public E poll();
public E pollFirst();
public E pop();
/**
* 獲取並刪除首結點值,若連結串列為空則丟擲異常
*/
public E remove() {
return removeFirst();
}
public E pop() {
return removeFirst();
}
public E removeFirst() {
final Node f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
/**
* 獲取並刪除首結點,若連結串列為空返回null
*/
public E poll() {
final Node f = first;
return (f == null) ? null : unlinkFirst(f);
}
public E pollFirst() {
final Node f = first;
return (f == null) ? null : unlinkFirst(f);
}
private E unlinkFirst(Node f) {
//獲取首結點值
final E element = f.item;
//獲取首結點後繼
final Node next = f.next;
//刪除首結點
f.item = null;
f.next = null; // help GC
//原後繼結點設為首結點
first = next;
//若後繼結點為null,尾結點設為null;若不是後繼結點的前驅結點設為null
if (next == null)
last = null;
else
next.prev = null;
//長度-1
size--;
modCount++;
return element;
}
/**
* 刪除指定位置結點
*/
public E remove(int index) {
//校驗index是否越界
checkElementIndex(index);
return unlink(node(index));
}
/**
* 刪除指定結點
*/
E unlink(Node x) {
// 獲取指定結點值
final E element = x.item;
// 獲取指定結點後繼
final Node next = x.next;
// 獲取指點結點前驅
final Node prev = x.prev;
// 若前驅為null,則後繼結點設為首結點
if (prev == null) {
first = next;
} else {
//若不是,指定結點後繼結點設為指定結點前驅結點後繼,指定結點的前驅設為null
prev.next = next;
x.prev = null;
}
//若後繼結點為空,則前驅結點設為尾結點
if (next == null) {
last = prev;
} else {
//若不是,指定結點前驅結點設為指定結點後繼結點前驅,指定結點的後繼設為null
next.prev = prev;
x.next = null;
}
//指定結點值設為null
x.item = null;
//長度-1
size--;
modCount++;
//返回被刪除的結點值
return element;
}
/**
* for迴圈從頭遍歷,刪除第一次出現的指定元素
*/
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;
}
/**
* 刪除從頭開始第一齣現的指定結點
*/
public boolean removeFirstOccurrence(Object o) {
return remove(o);
}
/**
* 刪除尾結點,若連結串列為空拋異常
*/
public E removeLast() {
final Node l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
/**
* 刪除尾結點,若連結串列為空返回null
*/
public E pollLast() {
final Node l = last;
return (l == null) ? null : unlinkLast(l);
}
/**
* 刪除尾結點
*/
private E unlinkLast(Node l) {
// 獲取尾結點值
final E element = l.item;
// 獲取尾結點前驅
final Node prev = l.prev;
// 刪除尾結點
l.item = null;
l.prev = null; // help GC
// 將原尾結點前驅設為尾結點
last = prev;
// 若原尾結點的前驅結點為空,則首結點設為null
if (prev == null)
first = null;
else
//若不為null,原原尾結點的前驅結點的後繼結點設為null
prev.next = null;
//長度-1
size--;
modCount++;
return element;
}
/**
* 刪除最後出現的結點
*/
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;
}
/**
* 清空LinkedList
*/
public void clear() {
//迴圈遍歷LinkedList,刪除所有結點
for (Node x = first; x != null; ) {
Node next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
//首尾結點置空
first = last = null;
//長度設為0
size = 0;
modCount++;
}
複製程式碼
轉換陣列
public Object[] toArray() {
// 宣告一個LinkedList長度的陣列
Object[] result = new Object[size];
int i = 0;
//for遍歷一一新增
for (Node x = first; x != null; x = x.next)
result[i++] = x.item;
return result;
}
複製程式碼
手撕簡單實現
public class LinkedList {
private int size;
private Node first;
private Node last;
public LinkedList() {
}
public void add(Object obj) {
final Node lastNode = last;
Node newNode = new Node(lastNode, obj, null);
if (lastNode == null) {
first = newNode;
} else {
lastNode.next = newNode;
}
last = newNode;
size++;
}
public void add(int index, Object obj) {
final Node node = getNode(index);
final Node preNode = node.pre;
if (index == size) {
add(obj);
return;
}
Node newNode = new Node(preNode, obj, node);
node.pre = newNode;
if (preNode == null) {
first = newNode;
} else {
preNode.next = newNode;
}
size++;
}
public Object get(int index) {
return getNode(index).item;
}
private Node getNode(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("越界");
}
if (index < size << 1) {
Node node = first;
for (int i = 0; i < index; i++) {
node = node.next;
}
return node;
} else {
Node node = last;
for (int i = size - 1; i > index; i--) {
node = node.pre;
}
return node;
}
}
public void set(int index, Object obj) {
Node node = getNode(index);
node.item = obj;
}
public void remove(int index) {
final Node node = getNode(index);
final Node preNode = node.pre;
final Node nextNode = node.next;
if (preNode == null) {
first = nextNode;
} else {
preNode.next = nextNode;
node.pre = null;
}
if(nextNode == null){
last = preNode;
} else {
nextNode.pre = preNode;
node.next = null;
}
node.item = null;
size--;
}
private static class Node {
private Object item;
private Node pre;
private Node next;
public Node(Node pre, Object item, Node next) {
this.item = item;
this.pre = pre;
this.next = next;
}
}
}
複製程式碼
結語
之前也介紹了ArrayList,總結下他們的區別:
①.ArrayList相當於動態陣列,LinkedList相當於雙向連結串列可以被當作堆疊、佇列、雙端佇列
②.ArrayList支援隨機訪問,而LinkedList需要一步步移動找到索引位置
③.ArrayList新增可能需要擴容預留記憶體,LinkedList不需要
④.ArrayList插入刪除操作(非尾部)需要陣列拷貝,LinkedList只需要修改結點的前驅後繼