本章是整理知识内容,为强化知识长期更新。
Linked List
- 链表即是由节点(Node)组成的线性集合,每个节点可以利用指针指向其他节点。它是一种包含了多个节点的、能够用于表示序列的数据结构。
- 单向链表: 链表中的节点仅指向下一个节点,并且最后一个节点指向空。
- 双向链表: 其中每个节点具有两个指针 p、n,使得 p 指向先前节点并且 n 指向下一个节点;最后一个节点的 n 指针指向 null。
- 循环链表:每个节点指向下一个节点并且最后一个节点指向第一个节点的链表。
- 时间复杂度:
- 索引:
O(n)- 搜索:
O(n)- 插入:
O(1)- 移除:
O(1)
LinkedList 是是一个有序的集合,是Collection的实现类之一。
概述
- 双链表 LinkedList 是 Java Collecion Framework成员之一。
- 双链表 是List和Deque的接口的实现。
- 在内部,它是使用双链表数据结构实现的。
- 它支持重复的元素。它没有实现RandomAccess接口。所以我们只能按顺序访问元素。它不支持随机访问元素。
- 相对与ArrayList它 删除 和添加 速度会快很多。但是遍历就要慢一些。
- 它以插入顺序存储或维护它的元素。
链表
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;
}
}
链表维护的最基本结构。在JVM中LinkedList存储并不会像数组一样按顺序来,存储的位置是随机的。
链接表结构
@Test
public void test(){
LinkedList list = new LinkedList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
System.out.println(list.toString());
System.out.println(list.getFirst()); // 第一个元素。
System.out.println(list.getLast()); // 最后一个元素
}
输出
[a, b, c, d]
a
d
linkedList的first == a next -> b next -> c next -> d next
案例-遍历LinkedList
@Test
public void test1(){
LinkedList list = new LinkedList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
System.out.println(list.toString());
Iterator<String> itr = list.iterator();
while (itr.hasNext()){
System.out.println(itr.next());
}
}
输出
[a, b, c, d]
a
b
c
d
都是Collection家族的使用迭代器进行遍历。
源码分析下linkedList
LinkedList中的常量和变量
// 容器大小
transient int size = 0;
// 头部节点引用
transient Node<E> first;
// 尾部节点引用
transient Node<E> last;
1、如何创建一个LinkedList实例。
@Test
public void test2(){
LinkedList list = new LinkedList();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
String[] arr = new String[]{"a","b","c","d"};
LinkedList list1 = new LinkedList(Arrays.asList(arr));
}
创建LinkedList的实例的方式有两种。LinkedList在这点上就与ArrayList不同,不需要指定大小。
//空构造方法 list1
public LinkedList() {
}
// list2
public LinkedList(Collection<? extends E> c) {
this();
// 将c Collection容器的元素都复制进来。
addAll(c);
}
2、add 方法
// 添加成功则返回true
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
final Node<E> l = last;
// 创建一个新的link last为头 prev为null
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
// 如果last是null,将e设置为first 反之放到尾部
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
// 将容器c的元素添加到当前容器中。
public boolean addAll(Collection<? extends E> c) {
// 把当前容器sezi 和 c传入
return addAll(size, c);
}
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index); //检查索引是否越界
Object[] a = c.toArray(); //将c 转换成数组
int numNew = a.length; //根据数组长度得到需要添加的数量
if (numNew == 0) //如果添加长度为0 就返回false.
return false;
Node<E> pred, succ; // 声明两个Link这其实是两个临时节点。
if (index == size) { // 从Index开始添加,如果index的位置等于当前容器的长度,则从last开始添加元素。
succ = null;
pred = last; // 容器last指向 pred
} else { // 从某一个link开始生成。
succ = node(index); // 获取index下标的元素。
pred = succ.prev; // 将succ.prev 指向 pred。
}
// 遍历数组中的元素,并按照顺序关系生成link
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开始添加元素的时候。
last = pred;
} else { //从某个元素开始添加元素。
pred.next = succ;
succ.prev = pred;
}
size += numNew; //更新size大小。
modCount++;
return true;
}
addAll方法会稍微麻烦点,举个栗子。
addAll 方法有两种情况,1、当前容器没有元素存在。2、当前容器已经存在元素。
@Test
public void test3() {
String[] arr = new String[]{"a" , "b"};
LinkedList list = new LinkedList(Arrays.asList(arr)); //情况一
arr = new String[]{"c" , "d"};
list.addAll(Arrays.asList(arr)); // 情况二
System.out.println(list);
}
第二次没有每次都展示出来,不过这图画的啰嗦点。其实上debug会直接点。
@Test
public void test5() {
String[] arr = new String[]{"a" , "b" , "c" , "d"};
LinkedList list = new LinkedList(Arrays.asList(arr));
list.add(1,"w"); // 在下标1的位置插入w
System.out.println(list);
}
输出
[a, w, b, c, d]
linkBefore
// 在指定索引位置添加元素。
public void add(int index, E element) {
checkPositionIndex(index); // 检查下标是否越界
if (index == size) //如果插入的位置是与长度相等,就吧当前last给他。
linkLast(element);
else
linkBefore(element, node(index));
}
/**
* Inserts element e before non-null Node succ.
* 在succ 的前面插入e.
*/
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++;
}
3、remove
这里只介绍两个带参数的remove方法。
@Test
public void test4(){
String[] arr = new String[]{"a","b","c","d"};
LinkedList list = new LinkedList(Arrays.asList(arr));
System.out.println(list.remove("a"));
System.out.println(list);
System.out.println(list.remove(1));
System.out.println(list);
}
输出
true
[b, c, d]
c
[b, d]
// 删除目标对象,若果删除成功就返回true反之返回false。
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));
}
// 根据下标寻找元素,找到并返回。
Node<E> node(int index) {
// assert isElementIndex(index);
// size >> 1 移位运算 相当于*0.5,根据索引下标的位置判断在集合前段还是后端前端就从first开始遍历反之从last开始遍历。
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;
}
}
/**
* Unlinks non-null 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;
}
unlink
4、poll方法
// 获取当前链表frist元素,如果不存在就返回null,存在该元素并删除。
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}