一、链表
链表(Linked list)是一种常见的基础数据结构,是一种线性表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer)。由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一种线性表顺序表快得多,但是查找一个节点或者访问特定编号的节点则需要O(n)的时间,而顺序表相应的时间复杂度分别是O(logn)和O(1)。
使用链表结构可以克服数组链表需要预先知道数据大小的缺点,链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理。但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大。
其实,上述重点可以总结为:链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序通过链表中的引用链接次序实现 。
链表的结构组合起来有8种链表结构。
由以下三类随机组合 :
1.单向、双向
2.带头、不带头(傀儡节点)
3.循环、非循环
而常见(需要掌握)的链表只有两种:
1.无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构
2.无头双向链表:在Java的集合框架库中 LinkedList 底层实现就是无头双向循环链表
以下代码可实现无头单向非循环链表:
class Node {
//Node: 节点(有data(保存数据)、next(保存下一个节点的地址(引用)))
public int data;
public Node next;//Node也是类型(引用)
public Node(int data) {
this.data = data;//初始化
//next初始化只能为null,无意义
}
}
public class MyLinedList {
public Node head;
public MyLinedList(Node head) {
this.head = null;
}
public MyLinedList() {
}
//头插法
public void addFirst(int data) {
//1.先拿到一个节点
Node node = new Node(data);
if(this.head == null) {
//第一次插入节点
this.head = node;
return;
}
//2.把它插入到head前面
node.next = this.head;//head = null
this.head = node;
}
//尾插法
public void addLast(int data) {
Node node = new Node(data);
if(this.head == null) {
//第一次插入节点
this.head = node;
return;
}
Node cur = this.head;
while(cur.next != null) {
cur = cur.next;
}
//cur指向节点尾巴
cur.next = node;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data) {
//Index: 要插入的节点名称
if (index < 0 || index > size()) {
throw new RuntimeException("index位置不合法");
}
if (index == 0) {
addFirst(data);
return;
}
if (index == size()) {
addLast(data);
return;
}
//1.让cur找到Index的前一个
Node prev = findIndex(index);
//2.先换node的地址,再换Index的前一个的地址
Node node = new Node(data);
node.next = prev.next;
prev.next = node;
}
private Node findIndex(int index) {
Node cur = this.head;
int count = 0;
while (count < index-1) {
cur = cur.next;
count++;
}
return cur;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
Node cur = this.head;
while (cur != null) {
if(cur.data == key) {
return true;
}
cur = cur.next;
}
return false;
}
//删除第一次出现关键字为key的节点
private Node findPrev(int key) {
//找要找节点的前一个节点prev
Node prev = this.head;
while (prev.next != null) {
if(prev.next.data == key) {
return prev;
}
prev = prev.next;
}
return null;
}
public void remove(int key) {
//prev为要删除节点的前一个;del为要删除的节点
Node prev = findPrev(key);
//如果要删除的节点为头节点:
if(key == this.head.data) {
this.head =head.next;
return;
}
//如果找不到要删除的节点:
if(prev == null) {
System.out.println("没有这个节点");
return;
}
//如果找到了,则直接跳过del节点为删除:
Node del = prev.next;
prev.next = del.next;
}
//删除所有 值为key的节点
public void removeAllKey(int key) {
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 = prev.next;//prev = cur;
cur = cur.next;
}
}
if(this.head.data == key) {
this.head = this.head.next;
}
}
//得到单链表的长度
public int size() {
Node cur = this.head;
int count = 0;
while (cur != null) {
cur = cur.next;
count++;
}
return count;
}
public void display() {
Node cur = this.head;
while (cur != null) {
System.out.print(cur.data + " ");
cur = cur.next;
}
}
public void clear() {
//清除(防止程序运行时发生内存泄漏)
//任何数据结构,都有可能发生内存泄漏
//如何查找内存泄漏:
//调试状态下 打开cmd 输入jps jmap-histo:live10912(每次不同) > 位置 此电脑里打开此位置 打开文件 查找node
//依次清除
while (this.head.next != null) {
Node del = this.head.next;
this.head.next = del.next;
}
this.head = null;
}
public Node findKthToTail(int k) {
//输入一个链表,输出该链表中倒数第k个结点
if (head == null) {
return null;
}
if(k <= 0 ) {
return null;
}
Node fast = this.head;
Node slow = this.head;
//1、让fast先走k-1步
while (k-1 > 0) {
if(fast.next != null) {
fast = fast.next;
k--;
}else {
System.out.println("没有该节点!");
return null;
}
}
//2、让两个引用 一起走 直到 fast.next == null
// slow 所指的位置就是倒数第K个节点
slow = slow.next;
fast = fast.next;
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
if(fast.next == null) {
return slow;
}
return null;
}
}
2.以下代码实现无头双向链表:
class ListNode {
public int val;
public ListNode prev;//前驱
public ListNode next;//后驱
public ListNode(int val) {
this.val = val;
}
public ListNode() {
}
}
public class DoubleLinkedList {
public ListNode head;//头
public ListNode last;//尾巴
//头插法
public void addFirst(int val) {
ListNode node = new ListNode(val);
if(this.head == null) {
this.head = node;
this.last = node;
return;
}
node.next = this.head;
this.head.prev = node;
this.head = node;
}
//尾插法
public void addLast(int val) {
ListNode node = new ListNode(val);
if(this.last == null) {
this.head = node;
this.last = node;
}else {
this.last.next = node;
node.prev = this.last;
this.last = node;
}
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int val) {
if(index < 0 || index > size()) {
throw new RuntimeException("index位置不合法");
}
if(index == 0) {
addFirst(val);
return;
}
if(index == size()) {
addLast(val);
return;
}
ListNode node = new ListNode(val);
ListNode cur = this.head;
while(index > 0) {
cur = cur.next;
index--;
}
node.next = cur;
node.prev = cur.prev;
cur.prev = node;
node.prev.next = node;
}
//查找是否包含关键字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) {
//判断当前cur是否为头节点
if(cur == this.head) {
this.head = this.head.next;
this.head.prev = null;
} else if(cur == this.last) {
this.last = this.last.prev;
this.last.next = null;
}else {
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
return;
}else {
cur = cur.next;
}
}
}
//删除所有值为 key 的节点
public void removeAllKey(int key) {
ListNode cur = this.head;
while (cur != null) {
if(cur.val == key) {
//判断当前cur是否为头节点
if(cur == this.head) {
this.head = this.head.next;
this.head.prev = null;
} else if(cur == this.last) {
this.last = this.last.prev;
this.last.next = null;
}else {
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
}
cur = cur.next;
}
}
public void clear() {
//将节点全部回收
this.head = null;
this.last = null;
}
public void display() {
ListNode cur = this.head;
while(cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
//得到单链表的长度
public int size() {
int len = 0;
ListNode cur = this.head;
while (cur != null) {
len ++;
cur = cur.next;
}
return len;
}
}
顺序表的详细介绍链接:https://blog.youkuaiyun.com/weixin_45975659/article/details/105715593
二、链表和顺序表的特点总结
链表:
优点:
1.任意位置插入删除时间复杂度为O(1)
2.没有增容问题,插入一个开辟一个空间
缺点:以节点为单位存储,不支持随机访问
顺序表:
优点:空间连续、支持随机访问
缺点:
1.中间或前面部分的插入删除时间复杂度O(N)
2.增容的代价比较大。