您现在的位置是:群英 > 开发技术 > 编程语言
C语言链表的概念及结构怎么理解,链表的分类有哪些
Admin发表于 2022-04-29 16:43:031082 次浏览
这篇文章给大家介绍了“C语言链表的概念及结构怎么理解,链表的分类有哪些”的相关知识,讲解详细,步骤过程清晰,对大家进一步学习和理解“C语言链表的概念及结构怎么理解,链表的分类有哪些”有一定的帮助,希望大家阅读完这篇文章能有所收获。下面就请大家跟着小编的思路一起来学习一下吧。


目录
  • 链表的概念及结构
    • 概念
    • 结构
  • 链表的分类
    • 单链表的实现(无头)
      • 双向链表的实现
        • 总结:链表和顺序表的区别

          链表的概念及结构

          概念

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

          结构

          代码

          struct Slist
          {
          	int* a;
          	struct Slist* next;
          };

          逻辑结构:

          物理结构:

          注意:

          • 从上图可以看出,链式结构在逻辑上是连续的,但是在物理上是不一定是连续的。
          • 这些结点一般是从堆上申请出来的。
          • 从堆上申请的空间,是按照一定的策划来分配的,两次申请的空间可能连续,大概率是不连续的。

          链表的分类

          实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:

          1. 单向或者双向
          ①单向

          ②双向

          2.带头或者不带头
          ①带头

          ②不带头

          3.循环或者非循环
          ①循环

          ②非循环

          虽然有这么多种结构的链表,但是我们实际中最常用的只有两种结构:
          1. 无头单向非循环链表

          2.带头双向循环链表

          1. 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。

          2. 带头双向循环链表:结构最复杂,一般用在单独存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然结构复杂,但是使用代码实现以后会发现结构会带来很多优势,实现反而简单了,后面我们代码实现了就知道了。

          单链表的实现(无头)

          单链表结构

          typedef int SLTDateType;
          
          typedef struct SListNode
          {
          	SLTDateType data;
          	struct SListNode* next;
          }SListNode;
          

          单链表需要的功能

          // 动态申请一个节点
          SListNode* BuySListNode(SLTDateType x);
          // 单链表打印
          void SListPrint(SListNode* plist);
          // 单链表尾插
          void SListPushBack(SListNode** pplist, SLTDateType x);
          // 单链表的头插
          void SListPushFront(SListNode** pplist, SLTDateType x);
          // 单链表的尾删
          void SListPopBack(SListNode** pplist);
          // 单链表头删
          void SListPopFront(SListNode** pplist);
          // 单链表查找
          SListNode* SListFind(SListNode* plist, SLTDateType x);
          // 单链表在pos位置之后插入x
          // 分析思考为什么不在pos位置之前插入?
          void SListInsertAfter(SListNode* pos, SLTDateType x);
          // 单链表删除pos位置之后的值
          // 分析思考为什么不删除pos位置?
          void SListEraseAfter(SListNode* pos);
          // 单链表的销毁
          void SListDestory(SListNode** pplist);
          

          功能实现

          SListNode* BuySListNode(SLTDateType x)
          {
          	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
          	if (newnode == NULL)
          	{
          		exit(-1);
          	}
          	newnode->data = x;
          	return newnode;
          }
          
          void SListPrint(SListNode* plist)
          {
          	if (plist == NULL)
          	{
          		printf("NULL\n");
          		return;
          	}
          	else
          	{
          		while (plist)
          		{
          			printf("%d->", plist->data);
          			plist = plist->next;
          		}
          		printf("NULL\n");
          	}
          }
          
          void SListPushBack(SListNode** pplist, SLTDateType x)
          {
          	SListNode* tail = *pplist;
          	SListNode* newnode = BuySListNode(x);
          	newnode->next = NULL;
          	if (tail == NULL)
          	{
          		*pplist = newnode;
          	}
          	else
          	{
          		while (tail->next)
          		{
          			tail = tail->next;
          		}
          		tail->next = newnode;
          	}
          }
          
          void SListPushFront(SListNode** pplist, SLTDateType x)
          {
          	SListNode* newnode = BuySListNode(x);
          	newnode->next = *pplist;
          	*pplist = newnode;
          }
          
          void SListPopBack(SListNode** pplist)
          {
          	assert(*pplist);
          	SListNode* tail = *pplist;
          	SListNode* Pretail = NULL;
          	if (tail->next == NULL)
          	{
          		*pplist = NULL;
          		return;
          	}
          	else
          	{
          		while (tail->next)
          		{
          			Pretail = tail;
          			tail = tail->next;
          		}
          		free(tail);
          		tail = NULL;
          		Pretail->next = NULL;
          	}
          }
          
          void SListPopFront(SListNode** pplist)
          {
          	assert(*pplist);
          	SListNode* front = *pplist;
          	*pplist = front->next;
          	free(front);
          	front = NULL;
          }
          
          SListNode* SListFind(SListNode* plist, SLTDateType x)
          {
          	assert(plist);
          	SListNode* pos = plist;
          	while (pos && pos->data != x)
          	{
          		pos = pos->next;
          	}
          	return pos;
          }
          
          void SListInsertAfter(SListNode* pos, SLTDateType x)
          {
          	assert(pos);
          	SListNode* newnode = BuySListNode(x);
          	newnode->next = pos->next;
          	pos->next = newnode;
          }
          
          void SListEraseAfter(SListNode* pos)
          {
          	assert(pos);
          	assert(pos->next);
          	SListNode* node = pos->next;
          	pos->next = node->next;
          	free(node);
          }
          
          void SListDestory(SListNode** pplist)
          {
          	SListNode* node = *pplist;
          	SListNode* PreNode = NULL;
          	while (node)
          	{
          		PreNode = node->next;
          		free(node);
          		node = PreNode;
          	}
          }
          
          

          双向链表的实现

          双向链表的结构

          SListNode* BuySListNode(SLTDateType x)
          {
          	SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
          	if (newnode == NULL)
          	{
          		exit(-1);
          	}
          	newnode->data = x;
          	return newnode;
          }
          
          void SListPrint(SListNode* plist)
          {
          	if (plist == NULL)
          	{
          		printf("NULL\n");
          		return;
          	}
          	else
          	{
          		while (plist)
          		{
          			printf("%d->", plist->data);
          			plist = plist->next;
          		}
          		printf("NULL\n");
          	}
          }
          
          void SListPushBack(SListNode** pplist, SLTDateType x)
          {
          	SListNode* tail = *pplist;
          	SListNode* newnode = BuySListNode(x);
          	newnode->next = NULL;
          	if (tail == NULL)
          	{
          		*pplist = newnode;
          	}
          	else
          	{
          		while (tail->next)
          		{
          			tail = tail->next;
          		}
          		tail->next = newnode;
          	}
          }
          
          void SListPushFront(SListNode** pplist, SLTDateType x)
          {
          	SListNode* newnode = BuySListNode(x);
          	newnode->next = *pplist;
          	*pplist = newnode;
          }
          
          void SListPopBack(SListNode** pplist)
          {
          	assert(*pplist);
          	SListNode* tail = *pplist;
          	SListNode* Pretail = NULL;
          	if (tail->next == NULL)
          	{
          		*pplist = NULL;
          		return;
          	}
          	else
          	{
          		while (tail->next)
          		{
          			Pretail = tail;
          			tail = tail->next;
          		}
          		free(tail);
          		tail = NULL;
          		Pretail->next = NULL;
          	}
          }
          
          void SListPopFront(SListNode** pplist)
          {
          	assert(*pplist);
          	SListNode* front = *pplist;
          	*pplist = front->next;
          	free(front);
          	front = NULL;
          }
          
          SListNode* SListFind(SListNode* plist, SLTDateType x)
          {
          	assert(plist);
          	SListNode* pos = plist;
          	while (pos && pos->data != x)
          	{
          		pos = pos->next;
          	}
          	return pos;
          }
          
          void SListInsertAfter(SListNode* pos, SLTDateType x)
          {
          	assert(pos);
          	SListNode* newnode = BuySListNode(x);
          	newnode->next = pos->next;
          	pos->next = newnode;
          }
          
          void SListEraseAfter(SListNode* pos)
          {
          	assert(pos);
          	assert(pos->next);
          	SListNode* node = pos->next;
          	pos->next = node->next;
          	free(node);
          }
          
          void SListDestory(SListNode** pplist)
          {
          	SListNode* node = *pplist;
          	SListNode* PreNode = NULL;
          	while (node)
          	{
          		PreNode = node->next;
          		free(node);
          		node = PreNode;
          	}
          }
          
          

          双向链表的功能

          //创建链表返回头结点
          LTNode* ListInit();
          // 双向链表销毁
          void ListDestory(LTNode* phead);
          // 双向链表打印
          void ListPrint(LTNode* phead);
          
          // 双向链表尾插
          void ListPushBack(LTNode* phead, LTDateType x);
          // 双向链表尾删
          void ListPopBack(LTNode* phead);
          // 双向链表头插
          void ListPushFront(LTNode* phead, LTDateType x);
          // 双向链表头删
          void ListPopFront(LTNode* phead);
          // 双向链表查找
          LTNode* ListFind(LTNode* phead, LTDateType x);
          // 双向链表在pos的前面进行插入
          void ListInsert(LTNode* pos, LTDateType x);
          // 双向链表删除pos位置的节点
          void ListErase(LTNode* pos);
          

          功能实现

          LTNode* ListInit()
          {
          	//哨兵位头结点
          	LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
          	if (phead == NULL)
          	{
          		printf("开辟空间失败!!!\n");
          		exit(-1);
          	}
          	phead->next = phead;
          	phead->prev = phead;
          	return phead;
          }
          
          void ListDestory(LTNode* phead)
          {
          	assert(phead);
          	LTNode* cur = phead;
          	LTNode* p = NULL;
          	LTNode* tail = phead->prev;
          	while (cur != tail)
          	{
          		p = cur;
          		cur = cur->next;
          		free(p);
          	}
          	free(tail);
          }
          
          void ListPrint(LTNode* phead)
          {
          	assert(phead);
          	LTNode* front = phead->next;
          	while (front != phead)
          	{
          		printf("%d ", front->data);
          		front = front->next;
          	}
          	printf("\n");
          }
          
          void ListPushBack(LTNode* phead, LTDateType x)
          {
          	assert(phead);
          	LTNode* tail = phead->prev;
          	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
          	if (newnode == NULL)
          	{
          		printf("开辟空间失败!!\n");
          		exit(-1);
          	}
          	newnode->data = x;
          	tail->next = newnode;
          	newnode->prev = tail;
          	newnode->next = phead;
          	phead->prev = newnode;
          }
          
          void ListPopBack(LTNode* phead)
          {
          	assert(phead);
          	assert(phead != phead->next);
          	LTNode* tail = phead->prev;
          	LTNode* TailFront = tail->prev;
          	TailFront->next = phead;
          	phead->prev = TailFront;
          	free(tail);
          }
          
          void ListPushFront(LTNode* phead, LTDateType x)
          {
          	assert(phead);
          	LTNode* next = phead->next;
          	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
          	if (newnode == NULL)
          	{
          		printf("开辟空间失败!!\n");
          		exit(-1);
          	}
          	newnode->data = x;
          	phead->next = newnode;
          	newnode->prev = phead;
          	newnode->next = next;
          	next->prev = newnode;
          }
          
          void ListPopFront(LTNode* phead)
          {
          	assert(phead);
          	assert(phead != phead->next);
          	LTNode* head = phead->next;//头结点
          	phead->next = head->next;
          	head->next->prev = phead;
          	free(head);
          }
          
          LTNode* ListFind(LTNode* phead, LTDateType x)
          {
          	assert(phead);
          	LTNode* cur = phead->next;
          	while (cur != phead)
          	{
          		if (cur->data == x)
          		{
          			return cur;
          		}
          		cur = cur->next;
          	}
          	return NULL;
          }
          
          void ListInsert(LTNode* pos, LTDateType x)
          {
          	assert(pos);
          	LTNode* posPrev = pos->prev;
          	LTNode* newnode = (LTNode*)malloc(sizeof(LTNode));
          	if (newnode == NULL)
          	{
          		printf("开辟空间失败!!\n");
          		exit(-1);
          	}
          	newnode->data = x;
          	posPrev->next = newnode;
          	newnode->prev = posPrev;
          	newnode->next = pos;
          	pos->prev = newnode;
          }
          
          void ListErase(LTNode* pos)
          {
          	assert(pos);
          	LTNode* posPrev = pos->prev;
          	LTNode* posNext = pos->next;
          	posPrev->next = posNext;
          	posNext->prev = posPrev;
          	free(pos);
          }
          

          总结:链表和顺序表的区别

          不同点 顺序表 链表存储空间上物理上一定连续逻辑上连续,物理上不一定连续随机访问支持不支持任意位置上插入或者删除元素可能需要移动元素,效率低下只需修改指针指向插入动态顺序表,空间不够时需要扩容没有容量的概念应用场景元素高效存储+频繁访问任意位置插入和删除频繁

          到此这篇关于C语言链表的文章就介绍到这了,更多相关C语言链表内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

          您可能感兴趣的文章:
          • C语言实现单链表的基本功能详解
          • 详解C语言之单链表
          • C语言实现循环双链表
          • 数据结构C语言链表的实现介绍

          以上就是关于“C语言链表的概念及结构怎么理解,链表的分类有哪些”的介绍了,感谢各位的阅读,希望文本对大家有所帮助。如果想要了解更多知识,欢迎关注群英网络,小编每天都会为大家更新不同的知识。

          免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

          标签: C语言链表
          相关信息推荐
          2022-06-02 17:18:15 
          摘要:php foreach可以遍历数组。foreach是专门为遍历数组而设计的语句,该语句遍历数组与数组下标无关。可用于遍历下标不连续的索引数组和以字符串为下标的关联数组;语法“foreach($arr as $k => $v){语句块;}”。
          2022-09-26 18:00:13 
          摘要:在本篇文章里小编给大家整理的是一篇关于PHP删除关联数组中键值的方法,有需要的朋友们可以学习参考下。
          2022-02-11 17:51:07 
          摘要:这篇文章给大家分享的是PHP中curl_init函数使用不了的处理方法。要解决这个问题并不难,下文给大家分享一个解决方法,希望对大家有帮助,接下来就跟随小编一起了解看看吧。
          云活动
          推荐内容
          热门关键词
          热门信息
          群英网络助力开启安全的云计算之旅
          立即注册,领取新人大礼包
          • 联系我们
          • 24小时售后:4006784567
          • 24小时TEL :0668-2555666
          • 售前咨询TEL:400-678-4567

          • 官方微信

            官方微信
          Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
          增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
          免费拨打  400-678-4567
          免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
          微信公众号
          返回顶部
          返回顶部 返回顶部