数据结构之链表
什么是链表?
就是一种链式存储数据的数据结构。与我们之前学习的数组不同,数组的每一个值的地址都是连续的,但是链表不是,链表中的每一个节点中,都会存储下一个节点的地址,因此,链表的地址不需要连续的一大块,可以是散乱的。数组的存储,在c语言中,数组名存储的就是数组的第一个位置的地址,那我们链表也需要一个地址来存储第一个节点所在的位置,所以,链表有一个头结点。那么头结点要不要存储数据呢?可以存储,也可以不存储。这个看个人需求。有的链表也可能会有尾结点,这个也看个人使用和要求了。
为什么要使用链表?
数组:
好处:查找十分简单,如果知道索引,那就是时间复杂度o(1)的事。
坏处:插入,删除的时候,数组的地址是无法改变的,所以这些操作只能从后往前覆盖,十分的麻烦。
链表:
好处:链表的插入与删除操作非常的简便,只需要修改节点的指针域存储位置即可。
坏处:链表的查找非常麻烦,每次查找都需要遍历完整条链表,耗时长。每个节点不仅需要存储数据,也需要存放节点的地址,空间消耗大。
链表与数组一对比,这简直不就是对方的互补版吗,哈哈哈。两种数据结构各有各自的优缺点,使用时看自己用来存储的数据时增加删除多还是查询比较多。
那可不可以将两者结合起来呢?当然是有的。像数据库中的存储就有使用到链表加数组的使用,感兴趣可以去查一查。
链表有哪些类别?
1.单链表
顾名思义,单链表,就是一条链表,从头到尾,一个节点接着一个节点。
我们来看单链表的构建
(下边没说就是默认head是不存储数据的)
基本结构
头结点data可以为空,单纯存储第一个节点的位置
尾结点则指向链表的最后一个位置,可以不new出来,因为他操作的就是最后一个节点的位置
class Node<T>{
T data;//保存数据
Node next;//保存下一个节点的地址
public Node(){//每构造一个Node默认设置他的指针域为空
this.next = null;
}
}
特别注意,在创建一个节点时,一定一定一定要将next域设为空。
构建方式
头插法:
Node newNode = new Node();
//head.next存储的是node地址
newNode.next = head.next;
head.next = newNode;
尾插法:
注意,tail是直接等于node 的,所以操作tail就相当于操作node
Node newNode = new Node();
tail.next = newNode;
tail = tail.next;
单链表的遍历
前序遍历:
就是构建的时候是1,2,3,4,5,6,7,8,9
输出的时候也是1,2,3,4,5,6,7,8,9
头结点不存数据
Node point = head;
//头结点未存数据
while(point.next != null){
//输出数据
System.out.print(point.next.data+" ");
//继续下一个
point = point.next;
}
System.out.println();
头结点存数据
Node point = head;
while(point != null){
//输出数据
System.out.print(point.data+" ");
//继续下一个
point = point.next;
}
System.out.println();
递归进行前序遍历(头结点不存数据)
static void preTraversal(Node head){
if(head.next == null){
return;
}
System.out.print(head.next.data+" ");
head = head.next;
preTraversal(head);
}
后序遍历:
就是构建的时候是1,2,3,4,5,6,7,8,9
输出时候是9,8,7,6,5,4,3,2,1
这里很有意思的一点就是,在单链表中我们无法得知当前节点的上一个节点的地址,那我们怎么进行后序遍历呢?
这就要题到另外一种数据结构,栈,这里简单介绍一下,栈是一种先进后出的数据结构,它需要等到最后一个数据进栈之后,才会出栈,所以,我们把节点一个一个的放进去,那它return的时候,就可以看到上一个节点的数据了。这样我们就可以进行链表的后序遍历。java在执行代码的时候,使用的就是栈这种数据结构。
static void sufTraversal(Node head){
if(head.next == null){
return;
}
head = head.next;
sufTraversal(head);
System.out.print(head.data+" ");
}
删除节点
要想删除某一个元素,我们就需要知道它前一个节点的位置,在删除的时候应该站在被删除元素的前一个节点进行操作了。有个空的头结点就是方便。
删除指定节点
public static void delete(Integer element,Node head){
Node point = head;
while(point.next != null){
if(point.next.data == element){
point.next = point.next.next;
}
point = point.next;
}
}
删除指定节点的下一个节点
public static void deleteAfter(Integer element,Node head){
Node point = head;
while(point!= null){
if(point.data == element){
point.next = point.next.next;
}
point = point.next;
}
}
删除指定节点的上一个节点
public static void deleteBefore(Integer element,Node head){
if(element == head.next.data){//无法删除第一个节点的上一个节点
System.out.println("error");
return;
}
Node point = head;
while(point.next.next != null){
if(point.next.next.data == element){
point.next = point.next.next;
}
point = point.next;
}
}
插入节点
指定节点后插入
public static void insertAfter(Integer element,Node head,Integer data){
Node point = head.next;
while (point != null) {
if (point.data == element) {
Node<Integer> newNode = new Node<>();
newNode.data = 10086;
newNode.next = point.next;
point.next = newNode;
}
point = point.next;
}
}
指定节点前插入
public static void insertBefore(Integer element,Node head,Integer data){
Node point = head;
while (point.next != null) {
if (point.next.data == element) {
Node<Integer> newNode = new Node<>();
newNode.data = 10086;
newNode.next = point.next;
point.next = newNode;
point = point.next;
}
point = point.next;
}
}
2.双链表
基本结构
class DoublyNode<T>{
public DoublyNode prior;//上一个节点的地址
public DoublyNode next;//下一个节点的地址
public T data;
//显示的赋值为空
public DoublyNode(){
this.next = null;
this.prior = null;
}
}
构建方式
我这里用头插法。
static DoublyNode constructList(){
DoublyNode<Integer> head = new DoublyNode();
for(int count = 1; count <= 20; count++){
DoublyNode<Integer> node = new DoublyNode<>();
node.data = count;
node.next = head.next;
if(count != 1){//只有一个节点的时候,head.next指向的是空,所以判断一下不然会报空指针异常
head.next.prior = node;
}
head.next = node;//这个要在上面一步之后欧,否则将会丢失head.next的节点地址
node.prior = head;
}
printLined(head);
return head;
}
双链表的遍历
static void printLined(DoublyNode head){
//从前往后遍历
while(head.next != null){
System.out.print(head.next.data+" ");
head = head.next;
}
System.out.println();
//从后往前遍历
while (head.prior!= null){
System.out.print(head.data+" ");
head = head.prior;
}
System.out.println();
}
删除节点
static void delete(Integer element,DoublyNode<Integer> head){
while(head.next != null){
if(head.next.data.equals(element)){
if(head.next.next!=null){
head.next = head.next.next;
head.next.prior = head;
}else {//删除最后一个节点时,删不掉,所以判断一下
head.next.prior = null;
head.next = null;
break;
}
}
head = head.next;
}
}
插入节点
1.1步
1.2步
2步
static void insertAfter(Integer position,Integer data,DoublyNode<Integer> head){
DoublyNode point = head.next;
while(point != null){
if(point.data == position){
DoublyNode<Integer> node = new DoublyNode<>();
node.data = data;
//这里先将新节点的前后指针域指向确定
node.next = point.next;
node.prior = point;
//再改变指定节点的后一个节点的原来的指向
if(node.next!=null){//如果是插入到最后一个点,就不会有下一个节点的前驱指针了,所以要判断
point.next.prior = node;
}
point.next = node;
}
point = point.next;
}
}
3.循环链表
头结点需要存储指定位置的数
基本结构
头尾相连,一个环,最后一个节点的指针域指向头结点
构建方式
这里举个循环链表的例子
约瑟夫环
啥是约瑟夫环?
约瑟夫环(约瑟夫问题)是一个数学的应用问题:已知 n 个人(以编号1,2,3…n分别表示)围坐在一张圆桌周围。从编号为 k 的人开始报数,数到 m 的那个人出圈;他的下一个人又从 1 开始报数,数到 m 的那个人又出圈;依此规律重复下去,直到剩余最后一个胜利者。
static int peopleNumber = 6 ; //n个人
static int interval = 5;//数m个人出圈
static Node constructList(){
Node node = new Node();
node.data = 1;
Node tail = node;
for (int size = 2; size <= peopleNumber; size++) {
Node newNode = new Node();
newNode.data = size;
tail.next = newNode;
tail = tail.next;
}
tail.next = node;//头尾相连
return tail;
}
循环链表的遍历
这里是不断的删除链表节点,最后一个值肯定是自己的next域指向自己了
static int star(Node head){
//cycle
int count = 1;//计数器,计录报数人的编号
while(head.next.data != head.next.next.data){
if(count == interval){
System.out.println(head.next.data);
head.next = head.next.next;
count = 1;
}
head = head.next;
count++;
}
return head.next.data;
}
学完链表建议做一些题目(狗头)
力扣:
链表反转(两种方式)
提示:直接遍历和使用尾递归法
查找链表中的环
提示:快慢指针
寻找环的入口
提示:将图画出来
删除链表的倒数第N个节点
提示:手动模拟+快慢指针