链表主要是为了弥补一些顺序表的缺点:
- 顺序表的插入删除操作时间复杂度为 O(N)
- 增容需要申请新空间,拷贝数据,释放旧空间,会有不小的消耗
- 增容一般都是成2倍增长,会浪费一定的空间
为了解决以上问题引入了链表结构,能够做到随用随取
链表的概念
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。
用图来简单描述一下链表
不带头节点的单向非循环链表
链表是由节点组成的,每一个节点都是一个 Node 对象,
带头节点单向不循环链表
链表的基本操作
头插法插入数据
public void addFirst(int data){
Node node = new Node(data);
if (this.head == null){
this.head = node;
return;
}
node.next = this.head;
this.head = node;
}
//测试头插法
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addFirst(10);
myLinkedList.addFirst(11);
myLinkedList.addFirst(12);
myLinkedList.addFirst(13);
myLinkedList.display();
}
打印链表的数据
public void display(){
Node cur = this.head;
while (cur != null){
System.out.print(cur.data + " ");
cur = cur.next;
}
}
测试一下
//测试头插法和打印
public static void main(String[] args) {
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addFirst(10);
myLinkedList.addFirst(11);
myLinkedList.addFirst(12);
myLinkedList.addFirst(13);
myLinkedList.display();
}
运行效果
尾插法插入数据
//尾插法
public void addLast(int data){
Node node = new Node(data);
Node cur = this.head;
if (cur == null){
this.head = node;
return;
}
while (cur.next != null){
cur = cur.next;
}
cur.next = node;
}
运行结果
查找某个值是否存在
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
Node cur = this.head;
while (cur != null){
if (cur.data == key){
return true;
}
cur = cur.next;
}
return false;
}
运行效果:
得到单链表的长度
//得到单链表的长度
public int size(){
int size = 0;
Node cur = this.head;
while (cur != null){
size++;
cur = cur.next;
}
return size;
}
运行效果
在任意位置插入节点(0 为第一个节点的位置)
举例,当我们要在 2 号位置 插入值为 88 的节点,可以通过下图来展示
插入后的指示图应如下图所示:
需要考虑的是应该先找到 2 号位置的前一个位置
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data){
if (index == 0){
addFirst(data);
return;
}
if (index == size()){
addLast(data);
return;
}
Node node = new Node(data);
Node cur = searchIndex(index);
node.next = cur.next;
cur.next = node;
}
//找到 index - 1 位置的节点并返回
private Node searchIndex(int index){
if (index < 0 || index > size()){
throw new RuntimeException("index位置不合法!");
}
Node cur = this.head;
while (index - 1 != 0){
cur = cur.next;
index--;
}
return cur;
}
运行效果:
在 2 号位置插入
在 0 号位置插入
在 5 号 位置插入
在 10 号 位置插入会返回一个异常
删除第一次出现的关键字为 key 的节点
//删除第一次出现关键字为key的节点
public void remove(int key){
if (this.head == null){
return;
}
if (this.head.data == key){
this.head = this.head.next;
return;
}
Node prev = searchPrev(key);
if (prev == null){
System.out.println("没有这个节点!");
return;
}
prev.next = prev.next.next;
}
//找到要删除的节点的前驱节点
private Node searchPrev(int key){
Node prev = this.head;
while (prev.next != null){
if (prev.next.data == key){
return prev;
}
prev = prev.next;
}
return null;
}
运行效果
删除所有值为 key 的节点
//遍历单链表一遍删除所有值为key的节点
public void removeAllKey(int key){
if (this.head == null){
return ;
}
Node prev = this.head;
Node cur =this.head.next;
while (cur != null){
if (cur.data == key){
prev.next = cur.next;
cur = cur.next;
}else {
prev = cur;
cur = cur.next;
}
if (this.head.data == key){
this.head = this.head.next;
}
}
运行结果
删除所有节点
/**
* 用来释放内存
*/
public void clear(){
this.head = null;
}
运行结果