LinkedList与链表
1.链表
1.1链表的概念
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序来实现的
1.2自己实现一个链表
1.2.1单链表的实现
public class MySingleList {
private ListNode head;
static class ListNode {
int value;
ListNode next;
public ListNode(int value) {
this.value = value;
}
}
public MySingleList(ListNode head) {
this.head = head;
}
//得到单链表的长度
public int size() {
int count = 0;
ListNode cur = this.head;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
//展示链表
public void display() {
ListNode cur = this.head;
while (cur != null) {
System.out.println(cur.value + " ");
cur = cur.next;
}
System.out.println();
}
//从某个结点开始输出链表
public void display(ListNode head) {
ListNode cur = head;
while (cur != null) {
System.out.println(cur.value + " ");
cur = cur.next;
}
System.out.println();
}
//清空链表
public void clear() {
this.head = null;
}
//头插法
public void addFirst(int data) {
ListNode cur = new ListNode(data);
cur.next = this.head;
this.head = cur;
}
//尾插法
public void addLast(int data) {
ListNode node = new ListNode(data);
ListNode cur = this.head;
if (head == null) {
this.head = node;
} else {
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data) throws Exception {
if (index < 0 || index > size()) {
throw new Exception("index位置不合法");
}
if (index == 0) {
addFirst(data);
}
if (index == size()) {
addLast(data);
}
ListNode cur = findIndexSubOne(index);
ListNode node = new ListNode(data);
node.next = cur.next;
cur.next = node;
}
//找出下标为index的结点
private ListNode findIndexSubOne(int index) {
ListNode cur = this.head;
while (index - 1 != 0) {
cur = cur.next;
index--;
}
return cur;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
ListNode cur = this.head;
while (cur != null) {
if (cur.value == key) {
return true;
}
cur = cur.next;
}
return false;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
if (this.head == null) {
return;
}
if (this.head.value == key) {
this.head = this.head.next;
return;
}
//找到要删除结点的前驱结点
ListNode cur = findPrevOfKey(key);
if (cur == null) {
System.out.println("没有要删除的结点");
return;
}
cur.next = cur.next.next;
}
//找出值为key的结点的前驱结点
private ListNode findPrevOfKey(int key) {
ListNode cur = this.head;
while (cur.next != null) {
if (cur.next.value == key) {
return cur;
}
cur = cur.next;
}
return null;
}
//删除所有值为key的节点
public void removeAllKey(int key) {
if (this.head == null) {
return;
}
ListNode cur = this.head.next;
ListNode prev = this.head;
while (cur != null) {
if (cur.value == key) {
prev.next = cur.next;
cur = cur.next;
} else {
prev = cur;
cur = cur.next;
}
}
if (this.head.value == key) {
this.head = this.head.next;
}
}
}
1.2.2实现双向链表
public class MyLinkedList {
static class ListNode {
public int val;
public ListNode prev;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
public ListNode tail;
//头插法
public void addFirst(int data) {
ListNode node = new ListNode(data);
if (this.head == null) {
this.head = node;
this.tail = node;
} else {
node.next = this.head;
this.head.prev = node;
head = node;
}
}
//尾插法
public void addLast(int data) {
ListNode node = new ListNode(data);
if (this.head == null) {
this.head = node;
this.tail = node;
} else {
tail.next = node;
node.prev = tail;
tail = node;
}
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data) {
if (index < 0 || index > size()) {
throw new RuntimeException("插入位置不合法");
}
if (index == 0) {
addFirst(data);
return;
}
if (index == size()) {
addLast(data);
return;
}
ListNode cur = FindIndexNode(index);
ListNode node = new ListNode(data);
node.next = cur;
cur.prev.next = node;
node.prev = cur.prev;
cur.prev = node;
}
private ListNode FindIndexNode(int index) {
ListNode cur = head;
while (index != 0) {
cur = cur.next;
index--;
}
return cur;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
ListNode cur = this.head;
while (cur != null) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
ListNode cur = this.head;
while (cur != null) {
//找到删除结点
if (cur.val == key) {
//如果删除结点为头结点
if (cur == head) {
head = head.next;
if (head != null) {
//头结点的前驱结点为空
head.prev = null;
} else {
//已经到达尾巴结点了
tail = null;
}
} else {
cur.prev.next = cur.next;
if (cur.next != null) {
cur.next.prev = cur.prev;
} else {
this.tail = cur.prev;
}
}
return;
}
cur = cur.next;
}
}
//删除所有值为key的节点
public void removeAllKey(int key) {
ListNode cur = this.head;
while (cur != null) {
if (cur.val == key) {
if (cur == head) {
head = head.next;
if (head != null) {
head.prev = null;
} else {
tail = null;
}
} else {
cur.prev.next = cur.next;
if (cur.next != null) {
cur.next.prev = cur.prev;
} else {
this.tail = cur.prev;
}
}
}
cur = cur.next;
}
}
//得到单链表的长度
public int size() {
int count = 0;
ListNode cur = this.head;
while (cur != null) {
cur = cur.next;
count++;
}
return count;
}
//展示链表
public void display() {
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
//清空链表
public void clear() {
ListNode cur = this.head;
while (cur != null) {
ListNode curNext = cur.next;
cur.next = null;
cur.prev = null;
cur = curNext;
}
this.head = null;
this.tail = null;
}
}
2.LinkedList
2.1LinkedList的基本概念
LinkedList的底层是双向链表结构(链表后面介绍),由于链表没有将元素存储在连续的空间中,元素存储在单独节点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高
2.2LinkedList的特点
- LinkedList实现了List接口
- LinkedList的底层使用了双向链表
- LinkedList没有实现RandomAccess接口,因此LinkedList不支持随机访问
- LinkedList的任意位置插入和删除元素时效率比较高,时间复杂度为O(1)
2.2LinkedList的使用
2.1.1构造方法
方法 | 解释 |
---|---|
LinkedList() | 无参构造 |
public LinkedList(collection<? extends E> c) | 使用实现了collection的集合构造LinkedList |
2.1.2常用方式
方法 | 解释 |
---|---|
boolean add(E e) | 尾插一个元素e |
void add(int index , E element) | 插入e到index位置 |
boolean addAll(Collection<? extends E> c) | 尾插c中的所有元素 |
E remove(int index) | 删除 index 位置元素 |
boolean remove(Object o) | 删除遇到的第一个 o |
E get(int index) | 获取下标 index 位置元素 |
E set(int index, E element) | 将下标 index 位置元素设置为 element |
void clear() | 清空 |
boolean contains(Object o) | 判断 o 是否在线性表中 |
int indexOf(Object o) | 返回第一个 o 所在下标 |
int lastIndexOf(Object o) | 返回最后一个 o 的下标 |
List subList(int fromIndex, int toIndex) | 截取部分 list |
2.2ArrayList和LinkedList的不同之处
- 在存储空间上
- ArrayList物理上一定连续
- LinkedList逻辑上连续,物理上不一定连续
- 随机访问
- ArrayList支持随机访问 O(1)
- LinkedList不支持随机访问0(N)
- 头插
- ArrayList需要搬移元素,效率O(N)
- LinkedList只需修改引用的指向,效率为O(1)
- 插入
- ArrayList插入空间不够时需要扩容
- LinkedList没有容量的概念
- 应用场景
- ArrayList应用于元素高效存储和频繁访问
- LinkedList应用于任意位置插入和删除频繁