数据结构——循环链表

1、循环链表示意图

2、相关操作

考察重点:头插、尾插、在指定位置插入数据

完整代码

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include <string.h>
#include<ctype.h>
#include<assert.h>
#include<stdlib.h>
typedef struct CNode
{
	int data;
	struct CNode* next;
}CNode, * CList;
//CList=CNode*
//初始化
void   InitCList(CList  plist)
{
	assert(plist != NULL);
	if (plist == NULL)
		return;
	//形成循环,头指向尾
	plist->next = plist;
}

//头插
bool  Insert_head(CList  plist, int val)
{
	assert(plist != NULL);
	if (plist == NULL)
		return false;
	//动态申请节点
	CNode* p = (CNode*)malloc(sizeof(CNode));
	assert(plist != NULL);
	//将数据放入新节点
	p->data = val;
	//插入新节点
	p->next = plist->next;
	plist->next = p;
	return true;
}

//尾插
bool  Insert_tail(CList plist, int val)
{
	assert(plist != NULL);
	if (plist == NULL)
		return false;
	//动态申请节点
	CNode* p = (CNode*)malloc(sizeof(CNode));
	assert(p != NULL);
	//将数据放入新节点
	p->data = val;
	//找尾巴
	CNode* q;
	for (q = plist; q->next != plist; q = q->next)
	{
		;
	}
	p->next = q->next;//或p->next=plist;先连接后面
	q->next = p;
	return true;
}

//获取数据节点的个数
int  Getlength(CList plist)
{
	assert(plist != NULL);
	if (plist == NULL)
		return -1;
	int count = 0;
	for (CNode* p = plist->next; p != plist; p = p->next)
		count++;
	return count;
}

//插入数据,在plist链表的pos位置插入val;
bool   Insert(CList plist, int pos, int val)
{
	assert(plist != NULL);
	if (plist == NULL)
		return false;
	if (pos<0 || pos>Getlength(plist))
		return false;
	//申请节点
	CNode* p = (CNode*)malloc(sizeof(CNode));
	assert(p != NULL);
	//放入数据
	p->data = val;
	//找位置
	int i = 0;
	CNode* q;
	for (q = plist; i < pos; i++, q = q->next)
	{
		;
	}
	//插入
	p->next = q->next;//先连接后面
	q->next = p;

}

//判空
bool   IsEmpyt(CList plist)
{
	return plist->next == plist;
}

//在plist中查找第一个key值,找到返回节点地址,没有找到返回NULL
CNode* Search(CList   plist, int key)
{
	assert(plist != NULL);
	if (plist == NULL)
		return NULL;
	for (CNode* p = plist->next; p != plist; p = p->next)
	{
		if (p->data = key)
			return p;
	}
	return NULL;
}

//删除pos位置的值
bool   DelPos(CList plist, int pos)
{
	assert(plist != NULL);
	if (plist == NULL)
		return false;
	CNode* p;
	int i;
	for (p = plist, i = 0; i < pos; i++, p = p->next)
	{
		;
	}
	CNode* q = p->next;//先保存
	p->next = p->next->next;
	free(q);
	return true;
}

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

//删除第一个val的值
bool  DelVal(CList plist, int vaSSl)
{
	CNode* p = GetPrio(plist, vaSSl);
	if (p == NULL)
		return false;
	CNode* q = p->next;
	p->next = q->next;
	free(q);
	return true;
}

//返回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)
{
	assert(plist != NULL);
	if (plist == NULL)
		return;
	for (CNode* p = plist->next; p != plist; p = p->next)
	{
		printf("%d ", p->data);
	}
	printf("\n");
}

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

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

int main()
{
	CNode head;
	InitCList(&head);
	for (int i = 0; i < 10; i++)
	{
		//Insert_head(&head, i);
		Insert_tail(&head, i);
	}
	Show(&head);
	/*Insert(&head, 1, 100);
	Show(&head);
	CNode*p=Search(&head, 2);
	if (p != NULL)
		printf("%d\n", p->data);
	else
		printf("没有找到!\n");
	DelVal(&head, 8);
	Show(&head);*/
	DelPos(&head, 0);
	Show(&head);
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CKX源路程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值