C语言学习第二十七天(单链表)

链表的概念以及结构

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

有几点需要注意:

1、链式结构在逻辑上是连续的,但是在物理上不一定连续

2、现实中的结点一般是从堆上申请出来的

3、从堆上申请空间,是按照一定的策略来分配的,两次申请的空间可能连续,也可能不连续

链表的分类

1、单向或者双向

2、带头或者不带头

3、循环或者非循环

但是我们常用的链表是:无头单向非循环链表和带头双向循环链表

链表的实现

cpp 复制代码
//无头+单向+非循环链表增删查改实现
typedef int SLTDataType;
typedef struct SListNode
{
	SLTDataType data;
	struct SListNode* next;
} SListNode;

//动态申请一个节点
SListNode* BuySListNode(SLTDataType x);
//单链表打印
void SListPrint(SListNode* plist);
//单链表尾插
void SListPushBack(SListNode** pplist, SLTDataType x);
//单链表的头插
void SListPushFront(SListNode** pplist, SLTDataType x);
//单链表的尾删
void SListPopBack(SListNode** pplist);
//单链表头删
void SListPopFront(SListNode** pplist);
//单链表查找
SListNode* SListFind(SListNode* plist, SLTDataType x);
//单链表在pos位置之后插入x
void SListInsertAfter(SListNode* pos, SLTDataType x);
//单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos);

接下来写一下每一个函数的实现代码

cpp 复制代码
//动态申请一个节点
SListNode* BuySListNode(SLTDataType x)
{
	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
	if (newnode == NULL)
	{
		perror("malloc fail");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}
cpp 复制代码
//单链表打印
void SListPrint(SListNode* plist)
{
	SListNode* cur = plist;
	while (cur != NULL)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}
cpp 复制代码
//单链表尾插
void SListPushBack(SListNode** pplist, SLTDataType x)
{
	SListNode* newnode = BuySListNode(x);
	if (*pplist == NULL)
	{
		*pplist = newnode;
	}
	else
	{
		SListNode* tail = *pplist;
		//找尾
		while (tail -> next)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}
cpp 复制代码
//单链表的头插
void SListPushFront(SListNode** pplist, SLTDataType x)
{
	SListNode* newnode = BuySListNode(x);
	newnode->next = *pplist;
	*pplist = newnode;
}
cpp 复制代码
//单链表的尾删
void SListPopBack(SListNode** pplist)
{
	//暴力的检查
	assert(*pplist);
	//温柔一点的检查
	/*if (*pplist == NULL)
	{
		return;
	}*/
	if ((*pplist)->next == NULL)
	{
		free(*pplist);
		*pplist = NULL;
	}
	else
	{
		SListNode* tail = *pplist;
		while (tail->next->next)
		{
			tail = tail->next;
		}
		free(tail->next);
		tail->next = NULL;
	}
}
cpp 复制代码
//单链表头删
void SListPopFront(SListNode** pplist)
{
	assert(*pplist);
	SListNode* next = (*pplist)->next;
	free(*pplist);
	*pplist = next;
}
cpp 复制代码
//单链表查找
SListNode* SListFind(SListNode* plist, SLTDataType x)
{
	SListNode* cur = plist;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}
cpp 复制代码
//单链表在pos位置之后插入x
void SListInsertAfter(SListNode* pos, SLTDataType x)
{
	assert(pos);
	SListNode* newnode = BuySListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}
cpp 复制代码
//单链表删除pos位置之后的值
void SListEraseAfter(SListNode* pos)
{
	assert(pos);
	if (pos->next == NULL)
	{
		return;
	}
	else
	{
		SListNode* nextNode = pos->next;
		pos->next = nextNode->next;
		free(nextNode);
	}
}
相关推荐
程序员编程指南18 分钟前
Qt 与 WebService 交互开发
c语言·开发语言·c++·qt·交互
程序员编程指南1 小时前
Qt 远程过程调用(RPC)实现方案
c语言·c++·qt·rpc·系统架构
Codeking__2 小时前
链表算法综合——重排链表
网络·算法·链表
花开月满西楼3 小时前
电子设计大赛【C语言核心知识点】讲解
c语言
sssammmm3 小时前
AI入门学习-模型评估示例讲解
人工智能·学习
erdongchen3 小时前
数组之一维数组
c语言
淮北4943 小时前
STL学习(四、队列和堆栈)
开发语言·c++·学习
落羽的落羽4 小时前
【C++】论如何封装红黑树模拟实现set和map
数据结构·c++·学习
用户6120414922134 小时前
C语言做的井字棋小游戏
c语言·后端·游戏
PerfumerKarma5 小时前
【WebGPU学习杂记】数学基础拾遗(2)变换矩阵中的齐次坐标推导与几何理解
学习·线性代数·矩阵