LinkedList
列表和双向链表的结合,允许值为null,检索时,从头或从尾开始,以更接近的索引为准.
一、LinkedList的定义
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
}
二、LinkedList的属性
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
// 链表的大小
transient int size = 0;
// 第一个节点
transient Node<E> first;
// 最后一个节点
transient Node<E> last;
}
三、LinkedList的构造器
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
}
四、LinkedList的主要方法
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable {
/**
* 添加元素
*/
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) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
/**
* 在链表指定位置添加元素
*/
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
final Node<E> pred = succ.prev;
final Node<E> newNode = new Node<>(pred, e, succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
/**
* 在链表头添加元素
*/
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);
}
/**
* 在链表尾部添加集合
*/
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;
// pred 伪头节点,succ 伪尾节点
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;
}
/**
* 删除第一个元素
*/
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
/**
* 删除最后一个元素
*/
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
/**
* 删除指定元素
*/
public boolean remove(Object o) {
}
/**
* 删除元素
*/
E unlink(Node<E> x) {
}
// Queue相关方法
// 返回头元素
public E peek() {}
public E peekFirst() {}
public E peekLast() {}
// 返回头元素,为null时抛出异常
public E element() {}
// 删除头元素
public E poll() {}
public E pollFirst() {}
public E pollLast() {}
// 删除头元素,为null时会抛出异常
public E remove() {}
// 增加元素
public boolean offer(E e) {}
public boolean offerFirst(E e) {}
public boolean offerLast(E e) {}
// 入栈
public void push(E e) {}
// 出栈
public E pop() {}
}
五、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;
}
}