数据结构:循环链表

1.头文件(clist.h)

#pragma once
//带头结点的循环链表,其尾节点的后继为头结点(不是NULL)
//节点的结构和单链表一样
typedef int ElemType;

typedef struct CNode//循环链表节点
{
	ElemType data;//数据
	struct CNode*next;//后继指针
}CNode ,*CList;
//初始化plist
void InitList(CList plist);

//往plist中头部插入数字val
bool Insert_head(CList plist, ElemType val);

//往plist中的尾部插入数字val
bool Insert_tail(CList plist, ElemType val);

//在plist中查找val值,找到返回该节点地址,失败返回NULL
CNode* Search(CList plist, ElemType val);

//删除plist中的第一个val
bool DeleteVal(CList plist, ElemType val);

//判断plist是否为空链表(没有数据节点)
bool IsEmpty(CList plist);

//获取plist长度,数据节点的个数
int GetLength(CList plist);

//获取plist链表的pos位置的值
bool GetElem(CList plist, int pos, int* rtval);//rtval:输出参数

//获取val的前驱
CNode* Prior(CList plist, ElemType val);

//获取val的后继
CNode* Next(CList plist, ElemType val);

//输出plist的所有数据
void Show(CList plist);

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

//销毁
void Destroy(CList plist);

2.函数实现文件(clist.cpp)

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"clist.h"

//初始化plist
void InitList(CList plist)
{
	assert(plist !=NULL );
	if (plist == NULL)
	{
		return ;
	}//数据域不使用
	plist->next = plist;
}
//头插
bool Insert_head(CList plist, ElemType val)
{
	
	CNode* p = (CNode*)malloc(sizeof(CNode));//1.动态申请一个新的节点
	assert(p != NULL);
	if (p == NULL)
		return false;
	p->data = val;//2.把数据存放在新节点中
	p->next = plist->next;//3.把新节点插入在头结点后面
	plist->next = p;
	return true;

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

//往plist中尾部插入数字val
bool Insert_tail(CList plist, ElemType val)
{
	CNode* p = (CNode*)malloc(sizeof(CNode));
	assert(p != NULL);
	if (p == NULL)
		return false;
	p->data = val;
	CNode*q = plist;
	for (; q->next != plist; q = q->next)
	{
		;
	}
	p->next = q->next;
	q->next = p;
	return true;

}

//在plist中查找val值,找到返回该节点地址,失败返回NULL
CNode* Search(CList plist, ElemType val)
{
	for (CNode* p = plist->next; p != plist; p = p->next)
	{
		if(p->data==val)
		return p;
	}
	return NULL;
}

//获取val的前驱
CNode* Prior(CList plist, ElemType val)
{
	for (CNode* p = plist; p->next != plist; p = p->next)
	{
		if (p->next->data == val)//寻找数据
		{
			return p;
		}
	}
		return NULL;//没有找的数据
	
}

//删除plist中的第一个val
bool DeleteVal(CList plist, ElemType val)
{
	CNode* p = Prior(plist, val);
	if (p == plist->next)
	{
		return NULL;
	}
	CNode* q = p->next;
	p->next = q->next;
	free(q);
	return true;
}
//判断plist是否为空链表(没有数据节点)
bool IsEmpty(CList plist)
{
	return plist->next == plist;
	//if (plist->next == plist)
	//{
	//	return true;
	//}
	//else 
	//{
	//	return false;
	//}
}
	
//获取plist长度,数据节点的个数
int GetLength(CList plist)
{
	int count=0;
	for (CNode* p = plist->next; p != plist; p = p->next)
	{
		count++;
	}
	return count;
}

//获取plist链表的pos位置的值
bool GetElem(CList plist, int pos, int* rtval)//rtval:输出参数
{
	if (pos<0 || pos>GetLength(plist))
	{
		return NULL;
	}
	CNode* p = plist->next;
	for (int i=0; i<pos; i++,p = p->next)
	{
		;
	}
	*rtval = p->data;
	return true;


}

//获取val的后继
CNode* Next(CList plist, ElemType val)
{
	for (CNode* p = plist->next; p->next != plist; p = p->next)
	{
		if (p->data == val)
		{
			return p -> next;
		}
	
	}
	return NULL;
}

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

//销毁
//void Destroy(CList plist)
//{
//	if (plist->next == plist)
//	{
//		return;
//	}
//	CNode* p = plist->next;
//	plist->next == plist;
//	CNode* q=p->next;
//	while (p != plist)
//	{
//		q= p->next;
//		free(p);
//		p = q;
//	}
//	
//}
void Destroy(CList plist)
{
	CNode* p ;
	while (plist->next != plist)
	{
		p = plist->next;
		plist->next = p->next;
		free(p);
	}
}

3.测试文件(test.cpp)

#include<stdio.h>
#include"clist.h"
int main()
{
	CNode head;
	InitList(&head);
	for (int i = 0; i < 6; i++)
	{
		//Insert_head(&head,i);
		Insert_tail(&head, i);
	}
	printf("长度:%d\n", GetLength(&head));//5
	Show(&head);

	int rtval;
	for (int i = 0; i < 6; i++)
	{
		if (GetElem(&head, i, &rtval))
			printf("%d,%d\n", i, rtval);
	}
	for (int i = -1; i < 7; i++)
	{
		CNode* p = Next(&head, i);//没有后继
		if (p == NULL)
			printf("%d没有后继\n", i);
		else
			printf("%d的后继数据是%d\n", i, p->data);
	}
	Show(&head);
	DeleteVal(&head, 0);
	DeleteVal(&head, 1);
	DeleteVal(&head, 2);
	Show(&head);
	Destroy(&head);
	if (IsEmpty(&head))
	{
		printf("空\n");
	}
	else {
		printf("不空\n");

	}
	
	return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值