html

数据结构:单循环链表的创建插入与删除

数据结构:单循环链表的创建·插入·删除

/**
  * @file name:	单循环链表的创建·插入·删除
  * @brief  实现单循环链表的创建删除插入的功能
  * @author liuliu@163.com
  * @date 2024/04/23
  * @version 1.0 :版本 
  * @note   noone
  * CopyRight (c)  2023-2024  liuliu@163.com   All Right Reseverd*/

//定义一个链表中节点的数据域类型
typedef int datatype_t;
//定义节点的类型并取别名
typedef struct Circular_LList{
	datatype_t data;
	struct Circular_LList *next;
}CirLList_t;
//创建一个空链表,含有一个头节点并初始化
CirLList_t *CirLList_Create()
{
	CirLList_t *Head=(CirLList_t *)calloc(1,sizeof(CirLList_t ));
	if(NULL==Head)
	{
		perror("calloc memory for Head is failed");
		exit(-1);
	}
	Head->next=Head;
	return Head;
}
//创建一个新的节点,并初始化成员变量
CirLList_t* CirLList_Newnode_Create(datatype_t data)
{
	CirLList_t * New=(CirLList_t *)calloc(1,sizeof(CirLList_t ));
	if(NULL==New)
	{
		perror("calloc memory for New is failed");
		return NULL;;
	}
	//为成员初始化
	New->data=data;
	New->next=NULL;
	return New;
}
//判断链表是否为空
bool CirLList_Isempoty(CirLList_t* Head)
{
	if(Head->next==NULL)
	{
		return ture;
	}
	return false;
	
}
//从头部插入一个节点
bool  CirLList_t* CirLList_HeadAdd(CirLList_t* Head,CirLList_t * New)
{
	if(New==NULL)
	{
		return false;
	}
	//当链表为空的情况
	if(CirLList_Isempoty(CirLList_t* Head))
	{
		Head->next=New;
		New->next=New;
		return ture;
	}
	//链表不为空的情况
	New->next=Head->next;
	Head->next=New;
	return ture;
}
//从尾部插入一个节点
bool  CirLList_t* CirLList_TailAdd(CirLList_t* Head,CirLList_t * New)
{
	CirLList_t* current=Head->next;//记录当前节点的地址
	if(New==NULL)
	{
		return false;
	}
	//当链表为空的情况
	if(CirLList_Isempoty(CirLList_t* Head))
	{
		Head->next=New;
		New->next=New;
		return ture;
	}
	//链表不为空的情况,遍历找出尾部节点
	while(current)
	{
		if(current->next==Head->next)
		{
			brake;
		}
		current=current->next;
	}
	//插入节点到尾部节点后面
	current->next=New;
	New->next=Head->next;
	return ture;
}
//从中间插入一个节点
bool CirLList_t* CirLList_MidAdd(CirLList_t* Head,CirLList_t * New,CirLList_t *Des)
{
	CirLList_t* current=Head->next;//记录当前节点的地址
	if(New==NULL)
	{
		return false;
	}
	//当链表为空的情况
	if(CirLList_Isempoty(CirLList_t* Head))
	{
		Head->next=New;
		New->next=New;
		return ture;
	}
	//链表不为空的情况,遍历找出目标节点
	while(current)
	{
		if(current->data==Des->data)
		{
			brake;
		}
		current=current->next;
	}
	//插入节点到目标节点后面
	New->next=current->next;
	current->next=New;
	return ture;
}
//从头部删除节点
void CirLList_HeadDel(CirLList_t* Head)
{
	CirLList_t* current=Head->next;//记录当前节点的地址
	CirLList_t* phead=Head->next//记录首地址的地址
	//遍历找到尾节点
	while(current)
	{
		if(current->next==Head->next)
		{
			brake;
		}
		current=current->next;
	}
	//删除节点并释放首节点地址
	current->next=Head->next->next;
	phead->next=NULL;
	Head->next=current->next;
	free(phead);
	return
	
}
//尾部删除节点
void CirLList_TailDel(CirLList_t* Head)
{
	CirLList_t* current=Head-->next;//记录当前节点的地址
	CirLList_t* prev=Head//记录当前地节点的直接前驱的地址
	CirLList_t* prev_current=prev//记录尾部节点的直接前驱的地址
	//遍历找到尾节点
	while(current)
	{
		if(current->next==Head->next)
		{
			prev_current=prev;
			brake;
		}
		current=current->next;
		prev=prev->next;
	}
	//删除节点并释放节点地址
	prev_current->next=Head->next;
	current->next=NULL;
	free(current);
	return	;
}
//从中间删除节点
void CirLList_MidDel(CirLList_t* Head,CirLList_t* Dest)
{
	CirLList_t* current=Head->next;//记录当前节点的地址
	CirLList_t* prev=Head//记录当前地节点的直接前驱的地址
	CirLList_t* prev_current=prev//记录要删除节点的直接前驱的地址
	//遍历找到要删除的目标节点并找到要删除节点的直接前驱
	while(current)
	{
		if(current->data==Dest->data)
		{		
			prev_current=prev;
			brake;
		}
		current=current->next;
		prev=prev->next;
	}
	//删除节点并释放节点地址
	prev_current->next=current->next;
	current->next=NULL;
	free(current);
	return	
}


posted @ 2024-04-23 20:17  大螺丝贼六  阅读(30)  评论(0编辑  收藏  举报
html