数据结构--循环链表

一.循环链表的设计

1.循环链表的结构设计:

typedef struct CNode{
int data;
struct CNode* next;
}CNode ,*CList;

2.循环链表的示意图:
3.循环链表和单链表的区别:

唯一区别,没有空指针,尾节点的后继为头,为循环之意.

二.循环链表的实现

cpp 复制代码
//初始化
	free(q);

	return true;
}

//返回key的前驱地址,如果不存在返回NULL;
CNode* GetPrio(CList plist, int key)
{
	CNode* p;
	for (p = plist; p->next != plist; p = p->next)
	{
		if (p->next ->data == key)
		{

			return p;
		}
	}
	return NULL;

}

//返回key的后继地址,如果不存在返回NULL;
CNode* GetNext(CList plist, int key)
{
	CNode* p = Search(plist, key);
	if (p == NULL)
		return NULL;
	return p->next;
}

//输出
void Show(CList plist)
{
	for (CNode* p = plist->next; p != plist; p = p->next)
	{
		printf("%d  ", p->data);
	}
	printf("\n");
}

//清空数据
void Clear(CList plist)
{
	Destroy(plist);
}

//销毁整个内存
void Destroy(CList plist)
{
	//总是删除第一个数据节点
	CNode* p;
	while (plist->next != plist)
	{
		p = plist->next;
		plist->next = p->next;
		free(p);
	}
}

三.循环链表的总结

循环链表其实和单链表是一样的操作,只是在处理的时候处理好尾节点即可,切记,遍历循环链表中不可出现NULL,若遍历的时候出现NULL就错了.

相关推荐
多多*1 天前
分布式系统中的CAP理论和BASE理论
java·数据结构·算法·log4j·maven
小白程序员成长日记1 天前
2025.11.10 力扣每日一题
数据结构·算法·leetcode
dragoooon341 天前
[优选算法专题六.模拟 ——NO.40~41 外观数列、数青蛙]
数据结构·算法·leetcode
.小小陈.1 天前
数据结构5:二叉树
数据结构
Laity______1 天前
指针(2)
c语言·开发语言·数据结构·算法
是苏浙1 天前
零基础入门C语言之C语言实现数据结构之顺序表经典算法
c语言·开发语言·数据结构·算法
太理摆烂哥1 天前
数据结构之红黑树
数据结构
hnjzsyjyj1 天前
洛谷 B4241:[海淀区小学组 2025] 统计数对 ← STL map
数据结构·stl map
泡沫冰@1 天前
数据结构(18)
数据结构
苏纪云1 天前
数据结构期中复习
数据结构·算法