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);
	}
}
相关推荐
dengqingrui1233 小时前
【树形DP】AT_dp_p Independent Set 题解
c++·学习·算法·深度优先·图论·dp
我的心永远是冰冰哒3 小时前
ad.concat()学习
学习
ZZZ_O^O3 小时前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
代码雕刻家4 小时前
数据结构-3.9.栈在递归中的应用
c语言·数据结构·算法
slomay5 小时前
关于对比学习(简单整理
经验分享·深度学习·学习·机器学习
hengzhepa5 小时前
ElasticSearch备考 -- Async search
大数据·学习·elasticsearch·搜索引擎·es
Kalika0-06 小时前
猴子吃桃-C语言
c语言·开发语言·数据结构·算法
代码雕刻家6 小时前
课设实验-数据结构-单链表-文教文化用品品牌
c语言·开发语言·数据结构
龙图:会赢的6 小时前
[C语言]--编译和链接
c语言·开发语言
小小洋洋7 小时前
BLE MESH学习1-基于沁恒CH582学习
学习