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);
	}
}
相关推荐
薛不痒41 分钟前
深度学习之优化模型(数据预处理,数据增强,调整学习率)
深度学习·学习
昵称已被吞噬~‘(*@﹏@*)’~1 小时前
【RL+空战】学习记录03:基于JSBSim构造简易空空导弹模型,并结合python接口调用测试
开发语言·人工智能·python·学习·深度强化学习·jsbsim·空战
千金裘换酒1 小时前
LeetCode反转链表
算法·leetcode·链表
我想我不够好。1 小时前
学到的知识点 1.8
学习
JoyCheung-2 小时前
Free底层是怎么释放内存的
linux·c语言
旖旎夜光2 小时前
Linux(9)
linux·学习
浩瀚地学3 小时前
【Java】常用API(二)
java·开发语言·经验分享·笔记·学习
chao_6666663 小时前
解决 PowerShell 中文乱码问题
网络·学习·powershell
喵了meme3 小时前
Linux学习日记24:Linux网络编程基础
linux·网络·学习
BullSmall4 小时前
《庄子》导读
学习