一、链表(Linked List)介绍
链表是有序的列表,但是它在内存中的存储形式如下:
- 链表是以节点的方式存储的,是链式存储;
- 每一个节点包含data域,next域:指向下一个节点;
- 如图:发现链表的各个节点不一定是连续存储;
- 链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定
单链表(带头结点) 逻辑结构示意图如下:
二、单链表
只能往一个方向查找的链表
1、单链表的应用实例
使用带 head 头的单向链表实现 –水浒英雄排行榜管理完成对英雄人物的增删改查操作
- 第一种方法在添加英雄时,直接添加到链表的尾部
思路分析示意图:
- 第二种方式在添加英雄时,根据排名将英雄插入到指定位置(如果有这个排名,则添加失败,并给出提示)
思路的分析示意图:
- 修改节点功能
思路(1) 先找到该节点,通过遍历, (2) temp.name = newHeroNode.name ; temp.nickname= newHeroNode.nickname - 删除节点
思路分析的示意图:
- 代码演示:
package com.narwal.linkedlist;
public class SingleLinkedListDemo {
public static void main(String[] args) {
//先创建节点
HeroNode hero1 = new HeroNode(1, "宋江", "及时雨");
HeroNode hero2 = new HeroNode(2, "卢俊义", "玉麒麟");
HeroNode hero3 = new HeroNode(3, "吴用", "智多星");
HeroNode hero4 = new HeroNode(4, "林冲", "豹子头");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.addByNo(hero1);
singleLinkedList.addByNo(hero4);
singleLinkedList.addByNo(hero3);
singleLinkedList.addByNo(hero2);
singleLinkedList.list();
SingleLinkedListDemo singleLinkedListDemo = new SingleLinkedListDemo();
System.out.println("反转后的链表为:");
singleLinkedListDemo.reverse(singleLinkedList.getHead());
singleLinkedList.list();
}
// 求单链表中节点的个数
public int getLength(HeroNode head) {
// 创建一个临时节点代替头节点
HeroNode temp = head;
int length = 0;
while (temp.next != null) {
length++;
temp = temp.next;
}
return length;
}
// 查找单链表中倒数第k个节点
public void findLastIndexNode(HeroNode head, int k) {
// 创建一个临时节点代替头节点
HeroNode temp = head;
int length = getLength(head);
int index = length - k + 1;
if (index <= 0 || index > length) {
System.out.println("未找到节点");
return;
}
for (int i = 0; i < index; i++) {
temp = temp.next;
}
System.out.printf("倒数第%d节点为:", k);
System.out.println(temp);
}
// 反转单链表
public void reverse(HeroNode head) {
// 创建一个临时节点代替头节点
if (head.next == null || head.next.next == null) {
return;
}
// 辅助变量
HeroNode node = head.next;
HeroNode next; //用于存储取出来的后一个节点
HeroNode newHead = new HeroNode(0, "", "");
while (node != null) {
next = node.next;
node.next = newHead.next;
newHead.next = node;
node = next;
}
head.next = newHead.next;
newHead.next = null;
}
// 反转打印单链表
public void reversePrint(HeroNode head) {
// 创建一个临时节点代替头节点
HeroNode temp = head;
if (temp.next == null) {
System.out.println("空链表");
return;
}
int size = getLength(head);
for (int j = 0; j < size; j++) {
for (int i = 0; i < size - j; i++) {
temp = temp.next;
}
System.out.println(temp);
temp = head;
}
}
}
// 定义SingleLinkedList管理我们的英雄
class SingleLinkedList {
// 此案初始化头结点,头结点不要动,不存放具体数据
private HeroNode head = new HeroNode(0, "", "");
public HeroNode getHead() {
return head;
}
// 按照node的顺序添加
public void addByNo(HeroNode node) {
// 创建一个临时节点代替头节点
HeroNode temp = head;
boolean flag = false; // 默认链表中没有相同序号的节点
// 遍历查找该节点要添加到的位置
while (true) {
if (temp.next == null) { // 遍历到了链表尾部
break;
}
if (temp.next.no > node.no) { // temp指向该节点的前一个位置
break;
} else if (temp.next.no == node.no) {
flag = true;
break;
}
temp = temp.next;
}
if (!flag) {
node.next = temp.next;
temp.next = node;
} else {
System.out.println("节点已存在,添加失败!");
}
}
// 删除指定节点
public void del(int no) {
// 创建一个临时节点代替头节点
HeroNode temp = head;
boolean flag = false; // 默认没有找到该节点
// 遍历查找该节点要添加到的位置
while (true) {
if (temp.next == null) { // 遍历到了链表尾部
break;
}
if (temp.next.no == no) { // temp指向要删除节点的前一个位置
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.next = temp.next.next;
} else {
System.out.println("要删除的节点不存在!");
}
}
// 修改节点
public void update(HeroNode node) {
HeroNode temp = head;
boolean flag = false; // 默认没有找到
while (true) {
if (temp.next == null) { // 遍历到了尾部
break;
}
if (temp.no == node.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = node.name;
temp.nickName = node.nickName;
} else {
System.out.println("未找到要修改的节点!");
}
}
// 添加节点到单项链表
// 1、找到当前链表的最后节点
// 2、将最后节点的next指向新的节点
public void add(HeroNode node) {
// 创建一个临时节点代替头节点
HeroNode temp = head;
while (true) {
if (temp.next == null) { // 找到了尾
temp.next = node;
break;
}
// 没有找到尾就往后延
temp = temp.next;
}
}
// 遍历所有的节点
public void list() {
// 创建一个临时节点代替头节点
HeroNode temp = head;
while (true) {
if (temp.next == null) { // 找到了尾
break;
}
// 没有找到尾就往后延
temp = temp.next;
System.out.println(temp);
}
}
}
class HeroNode {
public int no;
public String name;
public String nickName;
public HeroNode next;
public HeroNode(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
2、单链表面试题
(1)、求单链表中有效节点的个数
/**
* @param head 链表的头节点
* @return 返回的就是有效节点的个数
*/
public int getLength(HeroNode head) {
// 创建一个临时节点代替头节点
HeroNode temp = head;
int length = 0;
while (temp.next != null) {
length++;
temp = temp.next;
}
return length;
}
(2)、查找单链表中的倒数第 k 个结点
//思路
//1. 编写一个方法,接收 head 节点,同时接收一个k
//2. k 表示是倒数第 k 个节点
//3. 先把链表从头到尾遍历,得到链表的总的长度 getLength
//4. 得到 size 后,我们从链表头开始遍历 (size-index+1)个,就可以得到
//5. 如果找到了,则返回该节点,否则返回 nulll
// 查找单链表中倒数第k个节点
public void findLastIndexNode(HeroNode head, int k) {
// 创建一个临时节点代替头节点
HeroNode temp = head;
//判断如果链表为空,返回 null
if(head.next == null) { // 空链表
System.out.println("未找到!");
return;
}
int length = getLength(head);
int index = length - k + 1;
if (index <= 0 || index > length) {
System.out.println("未找到节点");
return;
}
for (int i = 0; i < index; i++) {
temp = temp.next;
}
System.out.printf("倒数第%d节点为:", k);
System.out.println(temp);
}
(3)、单链表的反转
- 思路分析图解
- 代码实现
// 反转单链表
public void reverse(HeroNode head) {
// 创建一个临时节点代替头节点
if (head.next == null || head.next.next == null) {
return;
}
// 辅助变量
HeroNode node = head.next;
HeroNode next; //用于存储取出来的后一个节点
HeroNode newHead = new HeroNode(0, "", "");
while (node != null) {
next = node.next; //先暂时保存当前节点的下一个节点,因为后面需要使用
node.next = newHead.next;
newHead.next = node;
node = next;
}
head.next = newHead.next;
newHead.next = null;
}
(4)、从尾到头打印单链表
// 反转打印单链表
public void reversePrint(HeroNode head) {
// 创建一个临时节点代替头节点
HeroNode temp = head;
if (temp.next == null) {
System.out.println("空链表");
return;
}
int size = getLength(head);
for (int j = 0; j < size; j++) {
for (int i = 0; i < size - j; i++) {
temp = temp.next;
}
System.out.println(temp);
temp = head;
}
}
三、双向链表
1、双向链表的操作分析和实现
1.1、管理单向链表的缺点分析:
- 单向链表,查找的方向只能是一个方向,而双向链表可以向前或者向后查找;
- 单向链表不能自我删除,需要靠辅助节点 ,而双向链表,则可以自我删除,所以前面我们单链表删除
节点时,总是找到 temp,temp 是待删除节点的前一个节点;
1.2、分析了双向链表如何完成遍历,添加,修改和删除的思路
- 遍历和单链表一样,只是可以向前,也可以向后查找;
- 添加 (默认添加到双向链表的最后)
1) 先找到双向链表的最后这个节点
2) temp.next = newHeroNode
3) newHeroNode.pre = temp;
- 修改 思路和 原来的单向链表一样.
- 删除
1) 因为是双向链表,因此,我们可以实现自我删除某个节点
2) 直接找到要删除的这个节点,比如 temp
3) temp.pre.next = temp.next
4) temp.next.pre = temp.pre;
1.3、代码实现
package com.narwal.linkedlist;
public class DoubleLinkedListDemo {
public static void main(String[] args) {
DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
HeroNode2 hero2 = new HeroNode2(2, "卢俊义", "玉麒麟");
HeroNode2 hero3 = new HeroNode2(3, "吴用", "智多星");
HeroNode2 hero4 = new HeroNode2(4, "林冲", "豹子头");
doubleLinkedList.addByNo(hero1);
doubleLinkedList.addByNo(hero3);
doubleLinkedList.addByNo(hero2);
doubleLinkedList.addByNo(hero4);
doubleLinkedList.list();
}
}
class DoubleLinkedList {
private HeroNode2 head = new HeroNode2(0, "", "");
public HeroNode2 getHead() {
return head;
}
// 按照node的顺序添加
public void addByNo(HeroNode2 node) {
// 创建一个临时节点代替头节点
HeroNode2 temp = head;
if (temp.next == null) { // 空链表
head.next = node;
node.pre = head;
return;
}
boolean flag = false; // 默认链表中没有相同序号的节点
// 遍历查找该节点要添加到的位置
while (true) {
if (temp.next == null) { // 遍历到了链表尾部
break;
}
if (temp.next.no > node.no) { // temp指向该节点的前一个位置
break;
} else if (temp.next.no == node.no) {
flag = true;
break;
}
temp = temp.next;
}
if (!flag) {
node.next = temp.next;
temp.next = node;
temp.next.pre = node;
node.pre = temp;
} else {
System.out.println("节点已存在,添加失败!");
}
}
// 删除指定节点
public void del(int no) {
/// 判断当前链表是否为空
if (head.next == null) {// 空链表
System.out.println("链表为空,无法删除");
return;
}
HeroNode2 temp = head.next; // 辅助变量(指针)
boolean flag = false; // 标志是否找到待删除的节点
while (true) {
if (temp == null) { // 已经到链表的最后
break;
}
if (temp.no == no) {
flag = true;
break;
}
temp = temp.next; // temp 后移,遍历
}
// 判断 flag
if (flag) { // 找到
temp.pre.next = temp.next;
if (temp.next != null) { // 删除的可能是最后一个节点
temp.next.pre = temp.pre;
}
} else {
System.out.printf("要删除的 %d 节点不存在\n", no);
}
}
// 修改节点
public void update(HeroNode2 node) {
HeroNode2 temp = head;
boolean flag = false; // 默认没有找到
while (true) {
if (temp.next == null) { // 遍历到了尾部
break;
}
if (temp.no == node.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = node.name;
temp.nickName = node.nickName;
} else {
System.out.println("未找到要修改的节点!");
}
}
// 添加节点到单项链表
// 1、找到当前链表的最后节点
// 2、将最后节点的next指向新的节点
public void add(HeroNode2 node) {
// 创建一个临时节点代替头节点
HeroNode2 temp = head;
while (true) {
if (temp.next == null) { // 找到了尾
temp.next = node;
node.pre = temp;
break;
}
// 没有找到尾就往后延
temp = temp.next;
}
}
// 遍历所有的节点
public void list() {
// 创建一个临时节点代替头节点
HeroNode2 temp = head;
while (true) {
if (temp.next == null) { // 找到了尾
break;
}
// 没有找到尾就往后延
temp = temp.next;
System.out.println(temp);
}
}
}
class HeroNode2 {
public int no;
public String name;
public String nickName;
public HeroNode2 next;
public HeroNode2 pre;
public HeroNode2(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
四、单向环形链表
1、应用场景
Josephu(约瑟夫、约瑟夫环)
问题
Josephu 问题为:设编号为 1,2,... n 的 n 个人围坐一圈,约定编号为 k(1<=k<=n)的人从 1 开始报数,数到 m 的那个人出列,它的下一位又从 1 开始报数,数到 m 的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。
提示:用一个不带头结点的循环链表来处理 Josephu 问题:先构成一个有 n 个结点的单循环链表,然后由 k 结点起从 1 开始计数,计到 m 时,对应结点从链表中删除,然后再从被删除结点的下一个结点又从 1 开始计数,直到最后一个结点从链表中删除算法结束。
2、单向环形链表介绍
2.1、约瑟夫问题
- 创建环形链表的思路图解
- 小孩出圈的思路分析图
- Josephu 问题的代码实现
package com.narwal.linkedlist;
public class Josepfu {
public static void main(String[] args) {
CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
circleSingleLinkedList.addBoy(5);
circleSingleLinkedList.showBoy();
circleSingleLinkedList.countBoy(1, 2,5);
}
}
// 创建一个环形的单向链表
class CircleSingleLinkedList {
// 创建一个 first 节点,当前没有编号;first始终指向第一个节点
private Boy first = null;
/**
* 小孩出圈
* @param startNo 表示从第几个小孩开始数数
* @param countNum 数几下
* @param nums 最初有多少小孩在圈中
*/
public void countBoy(int startNo, int countNum, int nums) {
// 先对数据进行校验
if (first == null || startNo < 1 || startNo > nums) {
System.out.println("参数输入有误, 请重新输入");
return;
}
Boy helper = first; // 辅助指针
while (helper.getNext() != first) { // 令辅助指针指向最后一个节点
helper = helper.getNext();
}
// 小孩报数前,先让first和helper移动k-1次
for (int i = 0; i < startNo - 1; i++) {
first = first.getNext();
helper = helper.getNext();
}
// 让first和helper指针同时移动m-1次;first节点指向的就是要出圈的节点
// 这里是一个循环操作,直到圈中只有一个节点
while (helper != first) {
for (int i = 0; i < countNum - 1; i++) {
first = first.getNext();
helper = helper.getNext();
}
// 这是first指向的节点就是要出圈 的小孩节点
System.out.printf("小孩%d 出圈\n", first.getNo());
//这时将 first 指向的小孩节点出圈
first = first.getNext();
helper.setNext(first);
}
System.out.printf("最后留在圈中的小孩编号%d \n", first.getNo());
}
// 添加小孩节点,构建成一个环形的链表
public void addBoy(int nums) {
// 参数校验
if (nums < 1) {
System.out.println("nums的值不正确!");
return;
}
Boy curBoy = null; // 辅助指针,帮助我们构建环形链表,始终指向最后一个节点
for (int i = 1; i <= nums; i++) {
Boy boy = new Boy(i);
if (i == 1) { // 先让第一个自己形成环
first = boy;
first.setNext(first);
curBoy = first;
} else {
curBoy.setNext(boy);
boy.setNext(first);
curBoy = boy;
}
}
}
// 遍历当前的环形链表
public void showBoy() {
// 判断是否为空
if (first == null) {
System.out.println("没有任何小孩!");
return;
}
// 由于first不能动,因此我们仍然需要一个辅助指针完成遍历
Boy curBoy = first;
while (true) {
System.out.printf("小孩的编号 %d \n", curBoy.getNo());
if (curBoy.getNext() == first) {// 说明已经遍历完毕
break;
}
curBoy = curBoy.getNext(); // curBoy 后移
}
}
}
class Boy {
private int no; // 编号
private Boy next; // 指向下一个节点,默认是null
public Boy(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Boy getNext() {
return next;
}
public void setNext(Boy next) {
this.next = next;
}
}
五、总结
- 对于链表而言,空链、链尾最好还是分开讨论;
- 无论是单向链表还是双向链表,其实都只是一种数据结构,和数组类似;