单循环链表

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>



typedef int ELEM_TYPE;

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

//循环链表(相比较单链表,唯一的区别,就是让最后一个节点的next域不再指向NULL,而是指向头结点)


//循环链表可执行的操作:(增删改查)
//初始化
void Init_clist(PClist pl);

//头插
bool Insert_head(PClist pl, int val);

//尾插
bool Insert_tail(PClist pl, int val);

//按位置插入
bool Insert_pos(PClist pl, int pos, int val);

//头删
bool Del_head(PClist pl);

//尾删
bool Del_tail(PClist pl);

//按位置删除
bool Del_pos(PClist pl, int pos);

//按值删除,删除遇到的第一个值为val的节点 如果值为val的节点不存在,则返回false
bool Del_val(PClist pl, int val);

//查找  查找值为val的第一个节点,找到则返回其节点地址,否则返回NULL
struct CNode* Search(PClist pl, int val);

//判空
bool IsEmpty(PClist pl);

//获取循环链表有效元素个数
int Get_length(PClist pl);

//打印
void Show(PClist pl);

//清空
void Clear(PClist pl);

//销毁1 一直头删的方式释放内存
void Destroy(PClist pl);

//销毁2
void Destroy2(PClist pl);

//寻找值为val的节点的前驱
PClist Get_prior(PClist pl,int val);

//寻找值为val的节点的后继
PClist Get_next(PClist pl,int val);
#include"clist.h"
//初始化
void Init_clist(PClist pl)
{
	assert(pl != NULL);
	if (pl == NULL)return;
	pl->next = pl;
}
//头插
bool Insert_head(PClist pl, int val)
{
	assert(pl != NULL);
	CNode* pnewnode = (CNode*)malloc(sizeof(CNode) * 1);
	if (pnewnode == NULL)exit(1);
	pnewnode->data = val;
	pnewnode->next = pl->next;
	pl->next = pnewnode;
	return true;
}
//尾插
bool Insert_tail(PClist pl, int val)
{
	assert(pl != NULL);
	CNode* p = pl;
	CNode* pnewnode = (CNode*)malloc(sizeof(CNode) * 1);
	if (pnewnode == NULL)exit(1);
	pnewnode->data = val;
	while (p->next!= pl)
	{
		p = p->next;
	}
	pnewnode->next = pl;
	p->next = pnewnode;
	return true;
}

//按位置插入
bool Insert_pos(PClist pl, int pos, int val)
{
	assert(pl != NULL);
	CNode* p = pl->next;
	int i = 1;
	while (i < pos)
	{
		p = p->next;
		i++;
	}
	CNode* pnewnode = (CNode*)malloc(sizeof(CNode) * 1);
	if (pnewnode == NULL)exit(1);
	pnewnode->data = val;
	if (p->next != pl)
	{
		pnewnode->next = p->next;
	}
	p->next = pnewnode;
	return true;
}

//头删
bool Del_head(PClist pl)
{
	assert(pl != NULL);
	if(pl->next==pl)
	{
	    return false;
	}
	CNode* p = pl->next;
	pl->next = p->next;
	free(p);
	return true;
}

//尾删
bool Del_tail(PClist pl)
{
	assert(pl != NULL&&pl->next!=pl);
	CNode* pre = pl;
	CNode* p = pl->next;
	while (p->next != pl)
	{
		pre = p;
		p = p->next;
	}
	pre->next = pl;
	free(p);
	return true;
}

//按位置删除
bool Del_pos(PClist pl, int pos)
{
	assert(pl!=NULL || pos < 0 || pos >= Get_length(pl));
	CNode* p = pl->next;
	int i = 1;
	while (i < pos)
	{
		p = p->next;

		i++;
	}
	CNode* q = p->next;
	p->next = q->next;
	free(q);
	return true;
}

//按值删除,删除遇到的第一个值为val的节点 如果值为val的节点不存在,则返回false
bool Del_val(PClist pl, int val)
{
	assert(pl != NULL);
	CNode* pre = pl;
	CNode* p = pre->next;
	while (pre->next != pl)
	{
		p = pre->next;
		if (p->data == val)
		{
			pre->next = p->next;
			free(p);
			return true;
			break;
		}
		else
			pre = pre->next;
	}
	return false;
}

//查找  查找值为val的第一个节点,找到则返回其节点地址,否则返回NULL
struct CNode* Search(PClist pl, int val)
{
	assert(pl != NULL);
	CNode* p = pl->next;
	while (p->next != pl)
	{
		if (p->data == val)
		{
			return p;
			break;
		}
		else
			p = p->next;
	}
	return NULL;
}

//判空
bool IsEmpty(PClist pl)
{
	return pl->next == NULL;
	return true;
}

//获取循环链表有效元素个数
int Get_length(PClist pl)
{
	int count = 0;
	CNode* p = pl->next;
	while (p != NULL)
	{
		p = p->next;
		count++;
	}
	return count;
}

//打印
void Show(PClist pl)
{
	assert(pl != NULL);
	CNode* p = pl->next;
	while (p != pl)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}
//销毁1 一直头删的方式释放内存 必须掌握
void Destroy(PClist pl)
{
	CNode* p = pl->next;
	while (p != pl)
	{
		Del_pos(pl, 0);
		p = p->next;
	}
}
//清空
void Clear(PClist pl)
{
    Destroy(pl);
}

//销毁2
void Destroy2(PClist pl)
{
	assert(pl != NULL && pl->next != NULL);
	CNode* p = pl->next;
	CNode* q;
	pl->next = NULL;
	while (p != NULL)
	{
		q = p->next;
		free(p);
		p = q;
	}
}

//寻找值为val的节点的前驱
PClist Get_prior(PClist pl,int val)
{
	assert(pl != NULL);
	CNode* pre = pl;
	CNode* p = pre->next;
	while (pre->next != pl)
	{
		p = pre->next;
		if (p->data == val)
		{
			return pre;
		}
		else
			pre = pre->next;
	}
	return NULL;
}

//寻找值为val的节点的后继
PClist Get_next(PClist pl, int val)
{
	assert(pl != NULL);
	CNode* p = Search(pl,val);
	return p=NULL?NULL:p->next;
}


int main()
{
	CNode clist;
	Init_clist(&clist);
	for (int i = 0; i < 10 ; i++)
	{
		Insert_tail(&clist, i + 1);
	}
	Show(&clist);
	Insert_pos(&clist,2,99);
	Show(&clist);
	Del_head(&clist);
	Show(&clist);
	Del_tail(&clist);
	Show(&clist);
	Del_pos(&clist,6);
	Show(&clist);
	Del_val(&clist, 99);
	Show(&clist);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值