文章目录
1、主要的成员变量及构造
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;
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
..........................
}
2、添加内容
实例:
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<Object> objects = new LinkedList<>();
objects.add(11);
objects.addFirst(0);
objects.addLast(33);
boolean offer = objects.offer(22);
}
}
执行add
// 使用到的内部类
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;
}
}
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
// 将成员变量的中的last赋值给l
final Node<E> l = last;
// 通过内部类实例化
final Node<E> newNode = new Node<>(l, e, null);
// 将新添加的节点为最后一个节点
last = newNode;
// 判断节点是否为null
if (l == null)
// 该节点为null时,则新添加的节点为第一个节点
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
示意图:
通过使用的内容类Node可以知道,该类存储的数据的结构是双向链表
执行addFirst
// 使用到的内部类
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;
}
}
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;
// 判断根节点是否为null
if (f == null)
// 为null的话,则将该节点设置为第一个节点
last = newNode;
else
// 不为null的话,则将当前的根节点的上一个节点指向新添加的节点
f.prev = newNode;
size++;
modCount++;
}
示意图:
a节点的指向的下一个节点是b节点,b节点指向的上一个节点是a; a节点的值是0,b节点的是值11
执行addLast
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++;
}
示意图:
a 节点的指向的下一个节点是b节点,b节点指向的上一个节点是a节点,b节点指向的指向的下一个节点是c节点,节点c指向的上一个节点是b节点;a节点是值是0,b节点的值是11,c节点的值是33
执行offer方法,执行的是add方法
public boolean offer(E e) {
return add(e);
}
3、查找内容
get
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;
}
}
上面的查找的范围,通过二分法的方式判断要查找的节点属于中间的节点的左边还是右边,从而判断从那边遍历寻找,提高了查找的速度
contains
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;
}
indexOf
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;
}
4、移除内容
remove
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;
// 获取x节点的值
final E element = x.item;
// 获取x节点的下一个节点
final Node<E> next = x.next;
// 获取x节点的上一个节点
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;
}