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);
	}
}
相关推荐
Want5953 分钟前
C/C++跳动的爱心
c语言·开发语言·c++
kongba0074 分钟前
Cursor提示词模板,开发GD32,C语言开发GD32 ARM单片机编程规范提示词 大厂风格代码规范
c语言·arm开发·单片机
Alidme41 分钟前
cs106x-lecture14(Autumn 2017)-SPL实现
c++·学习·算法·codestepbystep·cs106x
小王努力学编程41 分钟前
【算法与数据结构】单调队列
数据结构·c++·学习·算法·leetcode
ZxsLoves1 小时前
【【Systemverilog学习参考 简单的加法器验证-含覆盖率】】
学习·fpga开发
LaoZhangGong1231 小时前
STM32的“Unique device ID“能否修改?
c语言·经验分享·stm32·单片机·嵌入式硬件
明阳mark1 小时前
Ansible 学习笔记
笔记·学习·ansible
~kiss~2 小时前
python的thrift2pyi学习
windows·python·学习
Evaporator Core2 小时前
MATLAB学习之旅:数据建模与仿真应用
开发语言·学习·matlab
大米洗澡2 小时前
数字签名技术基础
python·学习·程序人生·面试·职场和发展