• 【数据结构】单链表


    前言

    在上一节中我们提出了顺序表的缺陷,为了解决这些问题,我们设计出了链表。

    一.链表

    1.链表的概念及结构

    链表是一种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的 。

    链表和顺序表的不同之处:顺序表的物理结构和逻辑结构都连续,而链表只需要逻辑结构连续,物理结构不需要连续。

    链表的结构图示如下:

    image-20220815235402897

    从上面的图示中我们可以看出,链表在结构上连续指的是链表的每个节点都存储着下一个节点的地址,我们可以根据这个地址找到链表的下一个节点,就像被一个链条连接起来一样。

    现实中链表的每一个节点都是在堆区上随机申请的,逻辑上连续的两个节点的地址在物理地址上并不连续,所以链表的节点没有物理结构的关系。

    2.物理结构和顺序结构

    物理结构就是指数据的逻辑结构在计算机中的存储形式,又叫存储结构。一般有两种,顺序存储(数组)和链式存储(链表)。

    逻辑结构就是指数据对象中数据元素之间的相互关系。一般有四种集合结构,线性结构,树形结构,图形结构。

    (1)集合结构
    集合结构中的数据元素除了同属一个集合外,它们之间没有任何关系。如图:

    image-20220815233701892

    (2)线性结构
    线性结构中的数据元素之间是一对一关系。如图:

    image-20220815233608083

    (3)树形结构
    树形结构中的数据元素之间存在一种一对多的层次关系。如图:

    image-20220815234646926

    (4)图形结构
    图形结构的数据元素是多对多的关系。如图:

    image-20220815234040322

    注意:每一个数据元素看做一个结点,用圆圈表示。元素之间的逻辑关系用线表示,如果这个关系是有方向的,那么用带箭头的连线表示。
    (1)顺序存储结构
    顺序存储结构是把数据元素存放在地址连续的存储单元里,其数据间的逻辑关系和物理关系是一致的。如图:

    image-20220815232905944

    这种存储结构说白了就是排队站位,比如数组就是这样的顺序存储结构。
    (2)链式存储结构
    链式存储结构是把数据元素存放在任意的存储单元里,这组存储单元可以连续,也可以不连续。数据元素的存储关系并不能反映其逻辑关系,因此需要用一个指针存放数据元素的地址,这样通过地址就可以找到相关联数据元素的位置。如图:

    image-20220815233013043

    链式存储就灵活多了,数据存在哪里不重要,只要有一个指针存放相应的地址就能找到它了。

    3.链表的分类

    在实际运用中,链表根据带头/不带头,循环/不循环,单向/双向这三种选择一共可以组合出8种结构。

    1. 不带头单向循环链表

    image-20220816010332816

    1. 不带头单向非循环链表

    image-20220816010353364

    1. 不带头双向循环链表

    image-20220816010415168

    1. 不带头双向非循环链表

    image-20220816010446669

    1. 带头单向循环链表

    image-20220816010511136

    1. 带头单向非循环链表

    image-20220816010547451

    1. 带头双向循环链表

    image-20220816010609794

    1. 带头双向非循环链表

    image-20220816010630966

    4.最常用的两种链表

    虽然链表有这么多中结构,但是我们实际中最常用还是以下两种结构:无头单向非循环链表和双向带头循环链表。

    4.1 无头单向非循环链表

    无头单向非循环链表结构最简单,一般不会单独用来存数据,实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等;另外这种结构在笔试面试中出现很多;其实如果不做特殊声明,一般情况下无头单向非循环链表指的就是我们的单链表。

    4.2 带头双向循环链表

    带头双向循环链表结构最复杂,一般用于单独存储数据;实际中我们使用的链表数据结构,都是带头双向循环链表;另外它虽然结构复杂,但是使用代码实现后会有很多优势,所以反而是链表中使用起来最简单的。

    二、单链表的实现

    由于单链表是其他结构链表学习的基础,且经常被用做其他数据结构的子结构,在笔试题中也最常被考到,所以下面我们用C语言来手动实现一个单链表,以此来加强我们对单链表的理解。

    1.结构的定义

    与顺序表一样,单链表也需要一个变量data来记录数据,并且我们应该对data的类型重命名,让我们的链表可以管理不同类型的数据;其次,由于单链表中需要存储下一个节点的地址,所以我们应该有一个指向结构体的指针。

    typedef int LSTDataType;
    typedef struct LinListNode
    {
    	LSTDataType data;
    	struct LinListNode* next;
    	//LSTNode* next;error 
    	//typedef定义的LSTNode在定义完之后才起作用
    }LSTNode;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.动态申请一个节点

    因为单链表的每一个节点都需要单独开辟空间申请,所以我们可以把动态申请节点封装成一个函数,避免在头插,尾插,在指定节点处插入数据重复实现申请节点。由于我们实现的单链表是不带头的,即单链表未申请节点前,单链表是空的,所以我们并不需要对它进行初始化操作,只需要定义一个指向NULL的结构指针plist。

    LSTNode* CreatLinListNode(LSTDataType x)
    {
    	LSTNode* newNode = (LSTNode*)malloc(sizeof(LSTNode));
    	if (newNode == NULL)
    	{
    		perror("malloc fail");
            //return NULL;
    		exit(-1);
    	}
    	newNode->data = x;
    	newNode->next = NULL;
    	return newNode;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.单链表头插

    特别注意:

    不管我们在什么地方插入数据,我们都需要传递二级指针。因为链表一开始是空的,所以我们在插入第一个数据的时候需要让 plist 指向我们新开辟的这一个节点,即头结点;而我们知道,要改变 int,需要传递 int*,要改变 int ,需要传递 int*,类比过来,这里的 plist 是一个结构体指针变量,我们想要改变它,让它从 NULL 变为第一个节点的地址,就需要传递结构体指针的地址,即二级指针才能实现。

    其次,我们在改变节点中的next指针的时候使用的是结构体指针,即一级指针,而并没有用到二级指针,这是因为我们修改节点中的next是对结构体进行操作,而要改变结构体我们只需要使用结构体指针即可,而不用像上面修改结构体指针一样使用二级指针。

    同时,结构体指针的地址是一定不为空的,因为即使是链表为空即 plist == NULL 的时候,&plist 也不等于空,所以我们需要对 pphead 进行断言,来保证代码的健壮性;而链表又是可能为空的,所以我们不能对 *pphead (即 plist) 进行断言。

    如果我们使用带头节点的单链表就不需要传递二级指针,因为不管我们如何对链表进行操作,头结点都始终不会改变。

    而且,插入数据我们一定需要创建一个新的节点,但是我们能不能在函数里面定义一个局部的节点。答案是不能。因为这个节点出了这个函数就被销毁了。所以我们需要定义一个函数创建一个全局的节点。

    在头部插入数据,我们需要先找到plist,然后使plist指向新开辟的节点,最后使新开辟的节点的next指向plist原来指向的节点或空指针,这样头插就完成了。

    void LinListPushFront(LSTNode** pphead, LSTDataType x)
    {
    	assert(pphead);
        //LSTNode* newNode  = malloc(sizeof(LSTNode));
    	LSTNode* newNode = CreatLinListNode(x);
    	newNode->next = *pphead;
    	*pphead = newNode;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4.单链表尾插

    在尾部插入数据我们需要先找到的尾结点的前一个节点,因为我们需要让前一个节点的next指针指向新开辟的节点,然后让新开辟的节点的next指向尾结点,这样才能让我们的链表链接起来。

    链表为空,插入第一个节点,要改变的是LinListNode*,用结构体指针的指针;链表不为空,尾插,要改变的是LinListNode,用结构体指针。

    由于我们的单链表只能找到从头结点找下一个节点的地址,想要找到尾节点前一个节点需要从头开始遍历,所以单链表尾插的效率是比较低的,时间复杂度为O(N),不过我们可以通过设计双向链表来解决这个问题。

    void LinListPushBack(LSTNode** pphead, LSTDataType x)
    {
    	assert(pphead);
    	LSTNode* newNode = CreatLinListNode(x);
    	//两种情况:链表为空,链表不为空
    	if (*pphead == NULL)
    	{
    		*pphead = newNode;
    	}
    	else
    	{
    		LSTNode* tail = *pphead;
    		while (tail->next != NULL)//while(tail != NULL) error
    		{
    			tail = tail->next;
    		}
    		tail->next = newNode;
    	}	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    5.单链表头删

    注意:这里是在头部删除数据,我们删除的数据可能是链表中唯一的数据,即可能会改变plist的指向(让plist重新指向NULL),所以我们再删除数据时,都要传递二级指针。

    另外,由于我们是删除数据,所以函数调用需要保证调用此函数是链表不为空,所以我们对*pphead进行断言,当链表为空时,删除元素提示报错信息。或者直接返回,不提示报错信息。我比较喜欢暴力一点的检查。

    void LinListPopFront(LSTNode** pphead)
    {
    	assert(pphead);
    	//温柔的检查
    	/*if (*pphead == NULL)
    	{
    		return;
    	}*/
    	//暴力的检查
    	assert(*pphead);
    
    	LSTNode* del = *pphead;
    	*pphead = (*pphead)->next;
    	free(del);
    	del = NULL;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    6.单链表尾删

    在尾部删除数据面临着和尾插一样的问题,需要改变前一个节点的next指针,使它指向NULL,所以时间复杂度为O(N)。

    void LinListPopBack(LSTNode** pphead)
    {
    	assert(pphead);
    
    	//温柔的检查
    	/*if (*pphead == NULL)
    	{
    		return;
    	}*/
    	//暴力的检查
    	assert(*pphead);
    
    	//尾删分为删一个节点
    	if ((*pphead)->next == NULL)
    	{
    		free(*pphead);
    		*pphead = NULL;
    	}
    	else //删多个节点
    	{
    		LSTNode* tail = *pphead;
    		//法一:
    		/*LSTNode* prev = NULL;
    		while (tail->next != NULL)
    		{
    			prev = tail;
    			tail = tail->next;
    		}
    		prev->next = NULL;
    		free(tail);
    		tail = NULL;*/
    		//法二:
    		while (tail->next->next != NULL)
    		{
    			tail = tail->next;
    		}
    
    		free(tail->next);
    		tail->next = NULL;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    7.单链表查找

    查找数据不用改变头结点,所以我们只需要传一级指针。

    LSTNode* LinListFind(LSTNode* phead, LSTDataType x)
    {
    	assert(phead);
    	LSTNode* cur = phead;
    	while (cur)
    	{
    		if (cur->data == x)
    		{
    			return cur;
    		}
    		cur = cur->next;
    	}
    	return NULL;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    8.在指定节点(pos)之前插入数据

    和尾插一样,我们需要从头遍历链表,找到pos节点的前一个节点,让该节点的next指向新开辟的节点,使得链表成功链接。

    void LinListInsertBefore(LSTNode** pphead, LSTNode* pos, LSTDataType x)
    {
    	assert(pphead);
    	assert(pos);
    	if (*pphead = pos)
    	{
    		LinListPushFront(pphead, x);
    	}
    	else
    	{
    		LSTNode* prev = *pphead;
    		while (prev->next != pos)
    		{
    			prev = prev->next;
    			// 暴力检查,pos不在链表中.prev为空,还没有找到pos,说明pos传错了
    			assert(prev);
    		}
    	
    		LSTNode* newNode = CreatLinListNode(x);
    		prev->next = newNode;
    		newNode = pos;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    9.在指定节点(pos)之后插入数据

    我们前面提到单链表在某一节点的前面插入数据时需要从头遍历寻找该节点的前一个节点,时间复杂度较高,为了提高单链表插入数据的效率,引入了在指定节点后插入数据的函数,提高效率。

    void LinListInsertAfter(LSTNode** pphead, LSTNode* pos, LSTDataType x)
    {
    	assert(pphead);
    	assert(pos);
    	LSTNode* newNode = CreatLinListNode(x);
    	newNode->next = pos->next;
    	pos->next = newNode;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    10.删除指定节点(pos)之后的数据

    和在pos位置后插入数据一样,为了提高效率,人们设计了一个在pos位置后删除数据的函数。

    void LinListEraseAfter(LSTNode* pos)
    {
    	assert(pos);
    	if (pos->next == NULL)
    	{
    		return;
    	}
    	else
    	{
    		LSTNode* next = pos->next;
    		pos->next = next->next;
    		free(next);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    11.删除指定节点(pos)位置的数据

    删除指定节点,我们仍然需要找到这个节点的前一个节点,让前一个节点指向指定节点的下一个节点,以此链接链表。

    void LinListErase(LSTNode** pphead, LSTNode* pos)
    {
    	assert(*pphead&&pphead);
    	assert(pos);
    	if (*pphead == pos)
    	{
    		LinListPopFront(pphead);
            //return;
    	}
    	else
    	{
    		LSTNode* prev = *pphead;
    		while (prev->next != pos)
    		{
    			prev = prev->next;
    			// 检查pos是不是链表中节点,参数传错了
    			assert(prev);
    		}
    		prev->next = pos->next;
    		free(pos);
    		pos = NULL;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    12.修改指定节点(pos)位置的数据

    修改数据不会改变头指针,所以传一级指针也可以,看你心情了。

    void LinListModify(LSTNode** pphead, LSTNode* pos, LSTDataType x)
    {
    	assert(pphead && pos);
    	LSTNode* cur = *pphead;
    	while (cur != pos)
    	{
    		assert(cur);
    		cur = cur->next;
    	}
    	cur->data = x;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    13.单链表打印

    打印数据不会改变头指针,所以这里传一级指针;但是这里我们不能断言,因为链表为空,打印也是正常的,打印NULL。

    void LinListPrint(LSTNode* phead)
    {
    	LSTNode* cur = phead;
    	while (cur != NULL)
    	{
    		printf("%d->",cur->data);
    		cur = cur->next;
    	}
        printf("NULL\n");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    14.单链表销毁

    销毁链表我们需要将plist 置空,所以这里我们传递二级指针。

    void LinListDestory(LSTNode** pphead)
    {
    	assert(pphead);
    	LSTNode* cur = *pphead;
    	while (cur != NULL)
    	{
    		//不能直接free当前节点,会找不到下一节点
    		//先保存下一节点,再free当前节点
    		LSTNode* next = cur->next;
    		free(cur);
    		cur = next;
    	}
    	*pphead = NULL;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    三.完整代码

    LinList.h

    #define _CRT_SECURE_NO_WARNINGS 1
    #pragma once
    
    #include
    #include
    #include
    
    typedef int LSTDataType;
    typedef struct LinListNode
    {
    	LSTDataType data;
    	struct LinListNode* next;
    	//LSTNode* next;error 
    	//typedef定义的LSTNode在定义完之后才起作用
    }LSTNode, *PLSTNode;
    
    //单链表打印
    void LinListPrint(LSTNode* phead);
    //void LinListPrint(PLSTNode phead);等价于上面的写法
    //动态申请一个节点
    LSTNode* CreatLinListNode(LSTDataType x);
    //单链表销毁
    void LinListDestory(LSTNode** pphead);
    //单链表头插
    void LinListPushFront(LSTNode** pphead, LSTDataType x);
    //单链表尾插
    void LinListPushBack(LSTNode** pphead, LSTDataType x);
    //单链表头删
    void LinListPopFront(LSTNode** pphead);
    //单链表尾删
    void LinListPopBack(LSTNode** pphead);
    //单链表查找
    LSTNode* LinListFind(LSTNode* phead, LSTDataType x);  
    //单链表在指定节点(pos)之后插入
    void LinListInsertAfter(LSTNode** pphead, LSTNode* pos, LSTDataType x);
    //单链表在指定节点(pos)之前插入
    void LinListInsertBefore(LSTNode** pphead, LSTNode* pos, LSTDataType x);
    //单链表修改指定节点(pos)位置的数据
    void LinListModify(LSTNode** pphead, LSTNode* pos, LSTDataType x);
    //单链表删除指定节点(pos)位置的数据
    void LinListErase(LSTNode** pphead, LSTNode* pos);
    //单链表删除指定节点(pos)后面的数据
    void LinListEraseAfter(LSTNode* pos);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    LinList.c

    #define _CRT_SECURE_NO_WARNINGS 1
    #include "LinList.h"
    
    void LinListPrint(LSTNode* phead)
    {
    	LSTNode* cur = phead;
    	while (cur != NULL)
    	{
    		printf("%d->",cur->data);
    		cur = cur->next;
    	}
        printf("NULL\n");
    }
    
    LSTNode* CreatLinListNode(LSTDataType x)
    {
    	LSTNode* newNode = (LSTNode*)malloc(sizeof(LSTNode));
    	if (newNode == NULL)
    	{
    		perror("malloc fail");
    		exit(-1);
    	}
    	newNode->data = x;
    	newNode->next = NULL;
    	return newNode;
    }
    
    void LinListPushFront(LSTNode** pphead, LSTDataType x)
    //改变plist,要用二级指针接收plist的地址
    {
    	assert(pphead);
    	//assert(*pphead);*pphead = plist,plist可能为空
    	//LSTNode* newNode  = malloc(sizeof(LSTNode));
    	LSTNode* newNode = CreatLinListNode(x);
    	newNode->next = *pphead;
    	*pphead = newNode;
    }
    
    void LinListPushBack(LSTNode** pphead, LSTDataType x)
    {
    	assert(pphead);
    	LSTNode* newNode = CreatLinListNode(x);
    	//两种情况:链表为空,链表不为空
    	if (*pphead == NULL)
    	{
    		*pphead = newNode;
    	}
    	else
    	{
    		LSTNode* tail = *pphead;
    		while (tail->next != NULL)//while(tail != NULL) error
    		{
    			tail = tail->next;
    		}
    		tail->next = newNode;
    	}	
    }
    
    void LinListPopFront(LSTNode** pphead)
    {
    	assert(pphead);
    	//温柔的检查
    	/*if (*pphead == NULL)
    	{
    		return;
    	}*/
    	//暴力的检查
    	assert(*pphead);
    
    	LSTNode* del = *pphead;
    	*pphead = (*pphead)->next;
    	free(del);
    	del = NULL;
    }
    
    void LinListPopBack(LSTNode** pphead)
    {
    	assert(pphead);
    
    	//温柔的检查
    	/*if (*pphead == NULL)
    	{
    		return;
    	}*/
    	//暴力的检查
    	assert(*pphead);
    
    	//尾删分为删一个节点
    	if ((*pphead)->next == NULL)
    	{
    		free(*pphead);
    		*pphead = NULL;
    	}
    	else //删多个节点
    	{
    		LSTNode* tail = *pphead;
    		//法一:
    		/*LSTNode* prev = NULL;
    		while (tail->next != NULL)
    		{
    			prev = tail;
    			tail = tail->next;
    		}
    		prev->next = NULL;
    		free(tail);
    		tail = NULL;*/
    		//法二:
    		while (tail->next->next != NULL)
    		{
    			tail = tail->next;
    		}
    
    		free(tail->next);
    		tail->next = NULL;
    	}
    }
    
    void LinListDestory(LSTNode** pphead)
    {
    	assert(pphead);
    	LSTNode* cur = *pphead;
    	while (cur != NULL)
    	{
    		//不能直接free当前节点,会找不到下一节点
    		//先保存下一节点,再free当前节点
    		LSTNode* next = cur->next;
    		free(cur);
    		cur = next;
    	}
    	*pphead = NULL;
    }
    
    LSTNode* LinListFind(LSTNode* phead, LSTDataType x)
    {
    	assert(phead);
    	LSTNode* cur = phead;
    	while (cur)
    	{
    		if (cur->data == x)
    		{
    			return cur;
    		}
    		cur = cur->next;
    	}
    	return NULL;
    }
    
    void LinListInsertBefore(LSTNode** pphead, LSTNode* pos, LSTDataType x)
    {
    	assert(pphead);
    	assert(pos);
    	if (*pphead = pos)
    	{
    		LinListPushFront(pphead, x);
    	}
    	else
    	{
    		LSTNode* prev = *pphead;
    		while (prev->next != pos)
    		{
    			prev = prev->next;
    			// 暴力检查,pos不在链表中.prev为空,还没有找到pos,说明pos传错了
    			assert(prev);
    		}
    	
    		LSTNode* newNode = CreatLinListNode(x);
    		prev->next = newNode;
    		newNode = pos;
    	}
    }
    
    void LinListInsertAfter(LSTNode** pphead, LSTNode* pos, LSTDataType x)
    {
    	assert(pphead);
    	assert(pos);
    	LSTNode* newNode = CreatLinListNode(x);
    	newNode->next = pos->next;
    	pos->next = newNode;
    }
    
    void LinListModify(LSTNode** pphead, LSTNode* pos, LSTDataType x)
    {
    	assert(pphead && pos);
    	LSTNode* cur = *pphead;
    	while (cur != pos)
    	{
    		assert(cur);
    		cur = cur->next;
    	}
    	cur->data = x;
    }
    
    void LinListErase(LSTNode** pphead, LSTNode* pos)
    {
    	assert(pphead&&(*pphead));
    	assert(pos);
    	if (pos == *pphead)
    	{
    		LinListPopFront(pphead);
            //return;
    	}
    	else
    	{
    		LSTNode* prev = *pphead;
    		while (prev->next != pos)
    		{
    			prev = prev->next;
    			// 检查pos不是链表中节点,参数传错了
    			assert(prev);
    		}
    		prev->next = pos->next;
    		free(pos);
    		pos = NULL;
    	}
    }
    
    void LinListEraseAfter(LSTNode* pos)
    {
    	assert(pos);
    	if (pos->next == NULL)
    	{
    		return;
    	}
    	else
    	{
    		LSTNode* next = pos->next;
    		pos->next = next->next;
    		free(next);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230

    Test.c

    #define _CRT_SECURE_NO_WARNINGS 1
    #include "LinList.h"
    
    void LinListTest1()
    {
    	LSTNode* plist = NULL;
    	//头插
    	LinListPushFront(&plist, 1);
    	LinListPushFront(&plist, 2);
    	LinListPushFront(&plist, 3);
    	LinListPrint(plist);
    	//尾插
    	LinListPushBack(&plist, 10);
    	LinListPushBack(&plist, 20);
    	LinListPushBack(&plist, 30);
    	LinListPrint(plist);
    	//头删
    	LinListPopFront(&plist);
    	LinListPopFront(&plist);
    	LinListPrint(plist);
    	//尾删
    	LinListPopBack(&plist);
    	LinListPopBack(&plist);
    	LinListPrint(plist);
    	//销毁
    	LinListDestory(&plist);
    }
    
    void LinListTest2()
    {
    	LSTNode* plist = NULL;
    	//头插
    	LinListPushFront(&plist, 5);
    	LinListPushFront(&plist, 3);
    	LinListPushFront(&plist, 1);
    	LinListPrint(plist);
    
    	//在pos位置前插入
    	LSTNode* pos = LinListFind(plist, 5);
    	if (pos != NULL)
    	{
    		LinListInsertBefore(&plist, pos, 20);
    	}
    	LinListPrint(plist);
    
    	pos = LinListFind(plist, 1);
    	if (pos != NULL)
    	{
    		LinListInsertBefore(&plist, pos, 40);
    	}
    	LinListPrint(plist);
    
    	//在pos位置后插入
    	pos = LinListFind(plist, 5);
    	if (pos != NULL)
    	{
    		LinListInsertAfter(&plist, pos, 50);
    	}
    	pos = LinListFind(plist, 6);
    	if (pos != NULL)
    	{
    		LinListInsertAfter(&plist, pos, 60);
    	}
    	LinListPrint(plist);
    
    	//查找并修改pos位置处的数据
        pos = LinListFind(plist, 1);
    	if (pos != NULL)
    	{
    		LinListModify(&plist, pos, 100);
    	}
    	LinListPrint(plist);
    	pos = LinListFind(plist, 5);
    	if (pos != NULL)
    	{
    		LinListModify(&plist, pos, 500);
    	}
    	LinListPrint(plist);
    
    	//销毁
    	LinListDestory(&plist);
    }
    
    void LinListTest3()
    {
    	LSTNode* plist = NULL;
    	//头插
    	LinListPushFront(&plist, 1);
    	LinListPushFront(&plist, 2);
    	LinListPushFront(&plist, 3);
    	LinListPushFront(&plist, 4);
    	LinListPushFront(&plist, 5);
    	LinListPushFront(&plist, 6);
    
    	LinListPrint(plist);
    
    	//删除pos位置处的数据
    	LSTNode* pos = LinListFind(plist, 1);
    	if (pos != NULL)
    	{
    		LinListErase(&plist, pos);
    	}
    	pos = LinListFind(plist, 3);
    	if (pos != NULL)
    	{
    		LinListErase(&plist, pos);
    	}
    	LinListPrint(plist);
    
    	//删除pos位置后的数据
    	pos = LinListFind(plist, 5);
    	if (pos != NULL)
    	{
    		LinListEraseAfter(pos);
    	}
    	LinListPrint(plist);
    	pos = LinListFind(plist, 6);
    	if (pos != NULL)
    	{
    		LinListEraseAfter(pos);
    	}
    	LinListPrint(plist);
    
    	//销毁
    	LinListDestory(&plist);
    }
    int main()
    {
    	//LinListTest1();
    	//LinListTest2();
    	LinListTest3();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133

    结语

    单链表这里就结束了,但是单链表是一个非常重要的知识,有很多经典的算法OJ题,以后会在【算法】栏目逐渐更新。

  • 相关阅读:
    【示波器专题】示波器有源探头为什么常常做成分体式?
    计算机毕业设计:基于html制作大学生网上报到系统响应式模板项目源码
    java之Number与Math及Random类
    C#流程控制————分支结构
    Android中使用Java操作List集合的方法合集,包括判读是否有重复元素等
    金融业信贷风控算法1-初等概率论
    Android 12(S) 图像显示系统 - SurfaceFlinger 之 VSync - 上篇(十六)
    《BPF Performance Tools —— 洞悉Linux系统和应用性能》学习笔记 —— 第一章 介绍(2)
    Hystrix原理
    JAVA基础(三十二)——反射之创建对象
  • 原文地址:https://blog.csdn.net/m0_64224788/article/details/126395605