链表
链表(Linked List)介绍
链表是有序的列表,但是它在内存中是存储如下:
小结:
- 链表是以节点的方式来存储,是链式存储
- 每个节点包含 data 域, next 域:指向下一个节点.
- 如上图:发现链表的各个节点不一定是连续存储.
- 链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定
单链表(带头结点) 逻辑结构示意图如下:
单链表
应用实例
使用带头head的单向链表实现 –水浒英雄排行榜管理
- 完成对英雄人物的增删改查操作
- 第一种方法在添加英雄时,直接添加到链表的尾部
- 第二种方式在添加英雄时,根据排名将英雄插入到指定位置(如果有这个排名,则添加失败,并给出提示)
图解(增删)
添加节点(末尾)
添加节点(顺序)
删除节点
代码实现(增删改查)
package com.atguigu.linkedlist;
/**
* @author Mustang
* @create 2022-05-06 10:17
*/
public class SingleLinkedListDemo7 {
public static void main(String[] args) {
HeroNode7 hero1 = new HeroNode7(1, "宋江", "及时雨");
HeroNode7 hero2 = new HeroNode7(2, "卢俊义", "玉麒麟");
HeroNode7 hero3 = new HeroNode7(3, "吴用", "智多星");
HeroNode7 hero4 = new HeroNode7(4, "林冲", "豹子头");
//创建链表
SingleLinkedList7 s7 = new SingleLinkedList7();
s7.addByOrder(hero1);
s7.addByOrder(hero3);
s7.addByOrder(hero4);
s7.addByOrder(hero2);
s7.list();
System.out.println();
// 删除之后的链表
s7.del(2);
s7.del(4);
s7.list();
System.out.println();
// 修改之后的链表
HeroNode7 newHero3 = new HeroNode7(3, "有用", "智多星");
s7.update(newHero3);
s7.list();
System.out.println();
}
}
//定义SingleLinkedList单链表类管理英雄
class SingleLinkedList7 {
//先初始化一个头节点,头节点不要动,不存放具体的数据
private HeroNode7 head = new HeroNode7(0, "", "");
//添加节点到单向链表
//思路,当不考虑编号顺序时:
//1. 找到当前链表的最后节点
//2. 将最后这个节点的next指向新的节点
public void add(HeroNode7 heroNode) {
//因为head节点不能动,因此需要一个辅助变量temp帮助遍历
HeroNode7 temp = head;
while (true) {
if (temp.next == null) {
break; //找到链表的最后
}
//如果没有找到最后, 就将temp后移
temp = temp.next;
}
//当退出while循环时,temp就指向了链表的最后
//将最后这个节点的next指向新的节点
temp.next = heroNode;
}
//第二种方式在添加英雄时,根据排名将英雄插入到指定位置
public void addByOrder(HeroNode7 heroNode) {
HeroNode7 temp = head;
//flag标志添加的编号是否存在,默认为false
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no > heroNode.no) {
break;//位置找到,就在temp的后面插入(从小到大)
} else if (temp.next.no == heroNode.no) {
flag = true; //说明编号存在
break;
}
temp = temp.next;
}
if (flag) {
System.out.printf("准备插入的英雄已存在,编号为[%d]无法加入\n", heroNode.no);
} else {
//插入到链表中, temp的后面
heroNode.next = temp.next;
temp.next = heroNode;
}
}
//删除节点
//比较时,是temp.next.no和需要删除的节点的no比较
public void del(int no) {
HeroNode7 temp = head;
//标志是否找到待删除节点
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
//找到节点的前一个节点
if (temp.next.no == no) {
flag = true;
break;
}
temp = temp.next;
}
//判断flag
if (flag) {
temp.next = temp.next.next;
} else {
System.out.printf("要删除的英雄节点不存在,编号为[%d]", no);
}
}
//修改节点的信息, 根据no编号来修改,即no编号不能改.
public void update(HeroNode7 newHeroNode) {
if (head.next == null) {
System.out.println("链表空");
return;
}
HeroNode7 temp = head.next;
//表示是否找到该节点
boolean flag = false;
while (true) {
if (temp == null) {
break;
}
if (temp.no == newHeroNode.no) {
flag = true; //找到
break;
}
temp = temp.next;
}
//根据flag 判断是否找到要修改的节点
if (flag) {
temp.name = newHeroNode.name;
temp.nickName = newHeroNode.nickName;
} else {
System.out.printf("没有找到编号[%d]的英雄节点,不能修改\n", newHeroNode.no);
}
}
//显示链表[遍历]
public void list() {
if (head.next == null) {
System.out.println("链表空");
return;
}
HeroNode7 temp = head.next;
while (true) {
if (temp == null) {
break;
}
//输出节点的信息
System.out.println(temp);
temp = temp.next;
}
}
}
//定义HeroNode,每个HeroNode对象就是一个节点
class HeroNode7 {
public int no;
public String name;
public String nickName;
public HeroNode7 next; //指向下一个节点
public HeroNode7(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode7{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
图解(面试题)
单链表反转:
从尾到头打印单链表:
代码实现(面试题)
- 求单链表中有效节点的个数
- 查找单链表中的倒数第k个结点
- 单链表的反转
- 从尾到头打印单链表(方式1:反向遍历 。 方式2:Stack栈)
在上面的代码的基础上,SingleLinkedList7类中增加方法
/**
* 1.获取到单链表的节点的个数(如果是带头结点的链表,需求不统计头节点)
*
* @param head 链表的头结点
* @return 返回节点个数
*/
public static int getLength(HeroNode7 head) {
//空链表
if (head.next == null) {
return 0;
}
int length = 0;
//不统计头节点
HeroNode7 temp = head.next;
while (temp != null) {
length++;
temp = temp.next;
}
return length;
}
/**
* 2.查找单链表中的倒数第k个结点
*
* @param index 表示是倒数第index个节点
* @param head 头节点
* @return 如果找到了,则返回该节点,否则返回null
*/
public static HeroNode7 findLastIndexNode(HeroNode7 head, int index) {
if (head.next == null) {
return null;
}
int size = getLength(head); //节点个数
//index校验
if (index <= 0 || index > size) {
return null;
}
HeroNode7 cur = head.next;
//for循环定位到倒数的index,(size-index)就是倒数第k个节点
for (int i = 0; i < size - index; i++) {
//第一次遍历完后,cur已经指向了第二个节点
cur = cur.next;
}
return cur;
}
/**
* 3.将单链表反转
*
* @param head 头节点
*/
public static void reverseList(HeroNode7 head) {
//如果当前链表为空,或者只有一个节点,无需反转,直接返回
if (head.next == null || head.next.next == null) {
return;
}
HeroNode7 cur = head.next;
HeroNode7 next = null; //记录临时节点
HeroNode7 reverseHead = new HeroNode7(0, "", "");
while (cur != null) {
//先暂时保存当前节点的下一个节点,后面需要使用
next = cur.next;
//将cur的下一个节点指向新的链表的最前端,链接新的链表
cur.next = reverseHead.next;
//将cur连接到新的链表上的第一个节点
reverseHead.next = cur;
//cur后移
cur = next;
}
//将head.next指向reverseHead.next,实现单链表的反转
head.next = reverseHead.next;
}
/**
* 4.逆序打印链表
* 方式一:将单链表反转再遍历,方式二:利用栈结构(先进后出)
* 方式二;将各个节点压入到栈中,然后利用栈的先进后出的特点,就实现了逆序打印的效果
*
* @param head 头节点
*/
public static void reversePrint(HeroNode7 head) {
if (head.next == null) {
return;
}
Stack<HeroNode7> stack = new Stack<>();
HeroNode7 cur = head.next;
//将链表的所有节点压入栈
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
//将栈中的节点进行打印,pop出栈
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
在测试类中增加如下代码:
int nodeNum = SingleLinkedList7.getLength(s7.getHead());
System.out.println("单链表节点数量:" + nodeNum);
HeroNode7 lastIndexNode = SingleLinkedList7.findLastIndexNode(s7.getHead(), 2);
System.out.println("查找的节点:" + lastIndexNode);
System.out.println("反转单链表:");
SingleLinkedList7.reverseList(s7.getHead());
s7.list();
System.out.println("逆序打印单链表:");
SingleLinkedList7.reversePrint(s7.getHead());
双链表
应用实例
- 使用带head头的双向链表实现 –水浒英雄排行榜
- 管理单向链表的缺点分析:
- 单向链表,查找的方向只能是一个方向,而双向链表可以向前或者向后查找。
- 单向链表不能自我删除,需要靠辅助节点;而双向链表,则可以自我删除,所以前面我们单链表删除时节点,总是找到temp,temp是待删除节点的前一个节点。
图解
思路:
分析双向链表的遍历,添加,修改,删除的操作思路
- 遍历,和单链表一样,只是可以向前,也可以向后查找
- 添加 (默认添加到双向链表的最后,也可以按顺序添加)
- 先找到双向链表的最后这个节点
- temp.next = newHeroNode
- newHeroNode.pre = temp;
- 修改,和单向链表一样.
- 删除
- 因为是双向链表,因此可以实现自我删除某个节点
- 直接找到要删除的这个节点,比如temp
- temp.pre.next = temp.next
- temp.next.pre = temp.pre;
代码实现
package com.atguigu.linkedlist;
/**
* @author Mustang
* @create 2022-05-06 13:06
*/
public class DoubleLinkedListDemo8 {
public static void main(String[] args) {
//进行测试
//先创建节点
HeroNode8 hero1 = new HeroNode8(1, "宋江", "及时雨");
HeroNode8 hero2 = new HeroNode8(2, "卢俊义", "玉麒麟");
HeroNode8 hero3 = new HeroNode8(3, "吴用", "智多星");
HeroNode8 hero4 = new HeroNode8(4, "林冲", "豹子头");
//创建链表
DoubleLinkedList8 d8 = new DoubleLinkedList8();
// 添加
d8.addByOrder(hero1);
d8.addByOrder(hero4);
d8.addByOrder(hero3);
d8.addByOrder(hero2);
d8.list();
System.out.println();
// 删除
d8.del(1);
d8.del(4);
d8.list();
System.out.println();
// 修改
HeroNode8 newHero3 = new HeroNode8(3, "有用", "智多星");
d8.update(newHero3);
d8.list();
}
}
//创建一个双向链表的类,管理双向链表
class DoubleLinkedList8 {
//头节点
private HeroNode8 head = new HeroNode8(0, "", "");
//增(无序,末尾添加),添加一个节点到双向链表的最后
public void add(HeroNode8 heroNode) {
HeroNode8 temp = head;
while (true) {
if (temp.next == null) {
break;
}
temp = temp.next;
}
//形成双向链表
temp.next = heroNode;
heroNode.pre = temp;
}
//增(有序,按排序添加),根据排名将英雄插入到指定位置
public void addByOrder(HeroNode8 heroNode) {
//temp是位于添加位置的前一个节点
HeroNode8 temp = head;
boolean flag = false;
while (true) {
if (temp.next == null) {
break;
}
if (temp.next.no > heroNode.no) {
break;
} else if (temp.next.no == heroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
System.out.printf("准备插入的英雄已存在,无法加入,编号为[%d]", heroNode.no);
} else {
//插入到链表中, temp的后面
//temp未到达链表末端,heronode连接相邻的后面的节点
if (temp.next != null) {
heroNode.next = temp.next;
temp.next.pre = heroNode;
}
//到达末端,但其实无论是否到达末端,都需要与前一个节点相连接
temp.next = heroNode;
heroNode.pre = temp;
}
}
//删,根据编号删除
public void del(int no) {
if (head.next == null) {
System.out.println("链表为空,无法删除");
return;
}
//找的是当前节点而不是前一个节点,实现自我删除
HeroNode8 temp = head.next;
boolean flag = false;
while (true) {
if (temp == null) {
break;
}
if (temp.no == no) {
flag = true; //找到
break;
}
temp = temp.next;
}
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(HeroNode8 newHeroNode) {
if (head.next == null) {
System.out.println("链表空");
return;
}
HeroNode8 temp = head.next;
boolean flag = false;
while (true) {
if (temp == null) {
break;
}
if (temp.no == newHeroNode.no) {
flag = true;
break;
}
temp = temp.next;
}
if (flag) {
temp.name = newHeroNode.name;
temp.nickName = newHeroNode.nickName;
} else {
System.out.printf("没有找到编号为[%d]的英雄节点,不能修改\n", newHeroNode.no);
}
}
//遍历
public void list() {
if (head.next == null) {
throw new RuntimeException("链表空");
}
HeroNode8 temp = head.next;
while (temp != null) {
System.out.println(temp);
temp = temp.next;
}
}
}
//定义HeroNode,每个HeroNode对象就是一个节点
class HeroNode8 {
public int no;
public String name;
public String nickName;
public HeroNode8 next; //指向下一个节点
public HeroNode8 pre; //指向前一个节点
public HeroNode8(int no, String name, String nickName) {
this.no = no;
this.name = name;
this.nickName = nickName;
}
@Override
public String toString() {
return "HeroNode7{" +
"no=" + no +
", name='" + name + '\'' +
", nickName='" + nickName + '\'' +
'}';
}
}
单向环形链表
应用场景
Josephu(约瑟夫、约瑟夫环) 问题
Josephu 问题为:设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,继续从出列的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。
如图所示:
图解
思路:用一个不带头结点的循环链表来处理Josephu 问题:先构成一个有n个结点的单循环链表,然后由k结点起从1开始计数,计到m时,对应结点从链表中删除,然后再从被删除结点的下一个结点又从1开始计数,直到最后一个结点从链表中删除算法结束。
1.构造一个单向环形链表
2.得到出圈顺序
代码实现
package com.atguigu.linkedlist;
/**
* @author Mustang
* @create 2022-05-06 21:50
*/
public class Josephu4 {
public static void main(String[] args) {
CircleSingleLinkedList4 c4 = new CircleSingleLinkedList4();
c4.addBoy(5);
c4.showBoy();
c4.countBoy(1, 2, 5);
}
}
//创建一个环形的单向链表
class CircleSingleLinkedList4 {
//创建一个first首节点,当前暂时没有编号
private Boy4 first;
/**
* 添加小孩节点,构建一个单向环形的链表
*
* @param nums 要添加的节点的数量
*/
public void addBoy(int nums) {
if (nums < 1) {
System.out.println("nums的值不正确");
return;
}
//辅助指针,帮助构建环形链表
Boy4 curBoy = null;
for (int i = 1; i <= nums; i++) {
Boy4 boy4 = new Boy4(i);
//如果是第一个小孩
if (i == 1) {
first = boy4;
first.setNext(first); //构成环
curBoy = first; //让curBoy指向第一个小孩
} else {
curBoy.setNext(boy4);
boy4.setNext(first); //构成环
curBoy = boy4;
}
}
}
//遍历当前的单向环形链表
public void showBoy() {
if (first == null) {
System.out.println("链表空, 没有任何小孩");
return;
}
//因为first不能动,因此仍然使用辅助指针完成遍历
Boy4 curBoy = first;
while (true) {
System.out.printf("小孩的编号[%d] \n", curBoy.getNo());
if (curBoy.getNext() == first) { //=first说明已经遍历完毕
break;
}
curBoy = curBoy.getNext();
}
}
/**
* 根据用户的输入,计算出小孩出圈的顺序
*
* @param startNo 表示从第几个小孩开始数,k
* @param countNum 表示数几下,m
* @param nums 表示最初有多少小孩在圈中
*/
public void countBoy(int startNo, int countNum, int nums) {
if (first == null || startNo < 1 || startNo > nums) {
System.out.println("参数输入有误,请重新输入!");
return;
}
//创建辅助指针,帮助完成小孩出圈,使其指向环形链表的最后节点
Boy4 helper = first;
while (true) {
if (helper.getNext() == first) {
break;
}
helper = helper.getNext();
}
//小孩报数前,先让first和helper移动 startNo-1次,动态改变首节点和尾节点
for (int i = 0; i < startNo - 1; i++) {
first = first.getNext();
helper = helper.getNext();
}
//小孩报数时,让first和helper指针同时移动 m-1次(first自己要数一次),然后出圈
while (true) {
if (helper == first) { //此时圈中只有一个节点
break;
}
//让first和helper指针同时移动 countNum-1次
for (int i = 0; i < countNum - 1; i++) {
first = first.getNext();
helper = helper.getNext();
}
//for循环之后,first指向的节点就是要出圈的小孩节点
System.out.printf("小孩[%d]出圈\n", first.getNo());
//删除first指向的节点
first = first.getNext();
helper.setNext(first);
}
System.out.printf("最后留在圈中的小孩的编号为[%d] \n", first.getNo());
}
}
//创建一个Boy类,表示一个节点
class Boy4 {
private int no;
private Boy4 next;
public Boy4(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Boy4 getNext() {
return next;
}
public void setNext(Boy4 next) {
this.next = next;
}
}