LinkedList
双向链表实现List和Deque接口。实现所有可选的列表操作,并允许所有元素null。
继承AbstractSequentialList,实现了List, Deque, Cloneable, java.io.Serializable接口
构造函数
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
以集合c中的元素作为链表中的元素
成员变量
1、transient int size = 0;
记录存储元素个数
2、transient Node first;
指向头节点
3、transient Node last;
指向尾节点
4、
方法
1、private void linkFirst(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++;
}
获取当前头节点,创建新节点将其放置于当前头节点的前边,调整新节点和原头节点的前节点和后节点,操作数+1
2、void linkLast(E 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++;
}
获取当前尾节点,创建新节点将其放置于当前尾节点的后边,调整新节点和原头节点的前节点和后节点,操作数+1
3、void linkBefore(E e, Node succ)
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++;
}
将新节点插入到指定节点的前面,调整新节点和指定节点的前节点和后节点,操作数+1
4、private E unlinkFirst(Node 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
5、private E unlinkLast(Node 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;
}
删除尾节点,尾引用向前移,操作数+1
6、E unlink(Node x)
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;
}
将指定节点移除链表,操作数+1
7、public E getFirst()
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
获取首节点元素,操作数+1
8、public E getLast()
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
获取尾节点元素
9、public E removeFirst()
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
移除首节点
10、public E removeLast()
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
移除尾元素
11、public void addFirst(E e)
public void addFirst(E e) {
linkFirst(e);
}
添加一个新元素并设为首节点
12、public void addLast(E e)
public void addLast(E e) {
linkLast(e);
}
添加一个新元素并设为尾节点
13、public boolean contains(Object o)
public boolean contains(Object o) {
return indexOf(o) != -1;
}
14、public int size()
public int size() {
return size;
}
15、public boolean add(E e)
public boolean add(E e) {
linkLast(e);
return true;
}
添加元素到链表尾部
16、public boolean remove(Object o)
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;
}
循环遍历,找到对应元素调用unlink()
17、public boolean addAll(Collection<? extends E> c)
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
18、public boolean addAll(int index, Collection<? extends E> 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 {
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;
}
检验元素是否合法,遍历加入节点。
19、public void clear()
public void clear() {
// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
// more than one generation
// - is sure to free memory even if there is a reachable Iterator
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++;
}
需要将所有节点的prev,item,next置为null
20、public E get(int index)
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
获取指定下标处的元素,检查是否越界
21、public E set(int index, E element)
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
获取指定下标处的元素,对其替换
22、public void add(int index, E element)
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
在指定下标前加入元素
23、public E remove(int index)
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
移除指定下标的元素
24、private boolean isElementIndex(int index)
private boolean isElementIndex(int index) {
return index >= 0 && index < size;
}
判断下标是否合法
25、private boolean isPositionIndex(int index)
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size;
}
26、private String outOfBoundsMsg(int index)
private String outOfBoundsMsg(int index) {
return "Index: "+index+", Size: "+size;
}
反馈出错的下标和链表的长度
27、private void checkElementIndex(int index)
private void checkElementIndex(int index) {
if (!isElementIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
28、private void checkPositionIndex(int index)
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
28、Node node(int index)
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;
}
}
判断在链表前半段还是后半段,之后遍历查找
29、public int indexOf(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;
}
遍历查找o
30、public int lastIndexOf(Object o)
public int lastIndexOf(Object o) {
int index = size;
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (x.item == null)
return index;
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (o.equals(x.item))
return index;
}
}
return -1;
}
反向遍历查找o
31、public ListIterator listIterator(int index)
public ListIterator<E> listIterator(int index) {
checkPositionIndex(index);
return new ListItr(index);
}
返回一个指定位置ListIterator
32、private LinkedList superClone()
private LinkedList<E> superClone() {
try {
return (LinkedList<E>) super.clone();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
33、public Object clone()
public Object clone() {
LinkedList<E> clone = superClone();
// Put clone into "virgin" state
clone.first = clone.last = null;
clone.size = 0;
clone.modCount = 0;
// Initialize clone with our elements
for (Node<E> x = first; x != null; x = x.next)
clone.add(x.item);
return clone;
}
34、public Object[] toArray()
public Object[] toArray() {
Object[] result = new Object[size];
int i = 0;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
return result;
}
35、public T[] toArray(T[] a)
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}
36、private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();
// Write out size
s.writeInt(size);
// Write out all elements in the proper order.
for (Node<E> x = first; x != null; x = x.next)
s.writeObject(x.item);
}
37、private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read in size
int size = s.readInt();
// Read in all elements in the proper order.
for (int i = 0; i < size; i++)
linkLast((E)s.readObject());
}
38、public Spliterator spliterator()
@Override
public Spliterator<E> spliterator() {
return new LLSpliterator<E>(this, -1, 0);
}
队列方法暂时跳过
内部类
Node
成员变量
1、E item;
存储的元素
2、Node next;
下一个节点的引用
3、Node prev;
前一个节点的引用
构造函数
Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}