双向循环链表创建

/*
*  @file_name 	  : Create DoubleCir_LList
*  @file_function : Creat a Double Circle LInked list
*  @author        : 9Tired
*  @date 		  :2026-8-20
*  @note		  : None
*  @CopyRight(c)  :13371080204@163.com 9Tired 2026-8-20
*/
//定义结构体方便更改数据类型
typedef int Datatype_t;
//定义结构体存储数据;前一个结点的地址;后一个结点的地址
typedef struct DoubleCircleLinkedList
{
	Datatype_t data;
	struct DoubleCircleLinkedList * prev;
	struct DoubleCircleLinkedList *next;
}DouCirLList_t;
//创建头结点
/*
*@ function_name : DouCirLList_CreateHead
*@ parameter	 : void
*@ retval		 :DouCirLList_t *
*@ function  	 :Creat a Head Node
*@ date			 : 2026-8-20
*@ note			 : None
*@ CopyRight(c)  : version 1.1.0  All rights reserved
*/
DouCirLList_t * DouCirLList_CreateHead(void)
{
	//为头结点申请空间并判断是否成功
	DouCirLList_t * Head=(DouCirLList_t *)calloc(1,sizeof(DouCirLList_t));
	if(Head == NULL){
		perror("Create HeadNode is failed");
		exit(-1);
	}
	//给头结点初始化
	Head->prev = Head;
	Head->next = Head;
	return Head;
}
/*
*@ function_name : DouCirLList_CreateNode
*@ parameter	 : Datatype_t *
*@ retval		 : DouCirLList_t *
*@ function  	 : Creat a New Node
*@ date			 : 2026-8-20
*@ note			 : None
*@ CopyRight(c)  : version 1.1.0  All rights reserved
*/
DouCirLList_t* DouCirLList_CreateNode(Datatype_t  data)
{
	//创建一个新节点并判断
	DouCirLList_t * New=(DouCirLList_t *)calloc(1,sizeof(DouCirLList_t));
		if(New == NULL){
		perror("Create NewNode is failed");
		exit(-1);
	}
	//对新结点进行初始化
	New->data = data;
	New->prev = New;
	New->next = New;
	return New;
}
/*
*@ function_name : DouCirLList_InsertHead
*@ parameter	 : Datatype_t * Head,Datatype_t data
*@ retval		 : bool
*@ function  	 : Insert a New Node in Head
*@ date			 : 2026-8-20
*@ note			 : None
*@ CopyRight(c)  : version 1.1.0  All rights reserved
*/
bool DouCirLList_InsertHead(DouCirLList_t*Head,Datatype_t data)
{
	//创建一个新结点
	DouCirLList_t* New = DouCirLList_CreatNode(data);
	//判断链表为空
	if(Head‑>next == Head)
	{
		Head -> next = New;
		return true;
	}
	//开始插入
	New->next=Head->next;
	New->prev=Head;
	Head->next->prev->next = New;
	Head->next->prev = New;
	Head->next=New;
	return true;
}
/*
*@ function_name : DouCirLList_InsertEnd
*@ parameter	 : DouCirLList_t* Head,Datatype_t data
*@ retval		 : bool
*@ function  	 : Insert a New Node in Head
*@ date			 : 2026-8-20
*@ note			 : None
*@ CopyRight(c)  : version 1.1.0  All rights reserved
*/
bool DouCirLList_InsertEnd(DouCirLList_t* Head,Datatype_t data)
{
	//创建一个新结点
	DouCirLList_t* New = DouCirLList_CreatNode(data);
	//判断链表为空
	if(Head‑>next == Head)
	{
		Head -> next = New;
		return true;
	}
	//开始插入
	New->prev = Head->next-prev;
	New->next = Head->next;
	Head->next->prev->next=New;
	return true;
}
/*
*@ function_name : DouCirLList_InsertMid
*@ parameter	 : DouCirLList_t* Head,Datatype_t data
*@ retval		 : bool
*@ function  	 : Insert a New Node in Mid
*@ date			 : 2026-8-20
*@ note			 : None
*@ CopyRight(c)  : version 1.1.0  All rights reserved
*/
bool DouCirLList_InsertMid(DouCirLList_t* Head,DouCirLList_t*dest,Datatype_t data)
{
	DouCirLList_t *Phead = Head;
	//创建一个新结点
	DouCirLList_t* New = DouCirLList_CreatNode(data);
	//判断链表为空
	if(Head‑>next == Head)
	{
		Head -> next = New;
		return true;
	}
	//便利找到位置
	while(Phead->next){
		Phead = Phead->next;
		if(Phead->next = dest){
			break;
		}
	}
	//进行插入(不需要管是否找到)
	New->next = Phead->next;
	New->prev = Phead;
	Phead->next->prev = New;
	Phea->next = New;
	return true;
}
/*
*@ function_name : DouCirLList_DelHead
*@ parameter	 : DouCirLList_t* Head
*@ retval		 : bool
*@ function  	 : Delete a New Node in Head
*@ date			 : 2026-8-20
*@ note			 : None
*@ CopyRight(c)  : version 1.1.0  All rights reserved
*/
bool DouCirLList_InsertMid(DouCirLList_t* Head Head)
{
	DouCirLList_t *Phead = Head;
	//判断链表为空
	if(Head->next == Head) return false;
	Head->next->next->prev = Head->next-prev;
	Head->next = Head->next->next;
	Phead->next->next = NULL;
	Phead->next->prev = NULL;
	free(Phead);
	return true;
}
/*
*@ function_name : DouCirLList_DelEnd
*@ parameter	 : DouCirLList_t* Head
*@ retval		 : bool
*@ function  	 : Delete a New Node in Tail
*@ date			 : 2026-8-20
*@ note			 : None
*@ CopyRight(c)  : version 1.1.0  All rights reserved
*/
bool DouCirLList_InsertMid(DouCirLList_t* Head)
{
	DouCirLList_t *Phead = Head;
	//判断链表为空
	if(Head->next == Head) return false;
	Phead = Head->next->prev;
	Head->next->prev = Head->next->prev->prev;
	Head->next->prev->next = Head->next;
	Phead->next = NULL:
	Phead->prev = NUULL:
	free(Phead);
	return true;
}
/*
*@ function_name : DouCirLList_DelEnd
*@ parameter	 : DouCirLList_t* Head,Datatype_t * data;
*@ retval		 : bool
*@ function  	 : Delete a New Node in Mid
*@ date			 : 2026-8-20
*@ note			 : None
*@ CopyRight(c)  : version 1.1.0  All rights reserved
*/
bool DouCirLList_InsertMid(DouCirLList_t* Head,Datatype_t * data)
{
	DouCirLList_t *Phead = Head;
	DouCirLList_t *Temp ;
	//判断链表为空
	if(Head->next == Head) return false;
	while(Phead->next){
		Phead = Phead->next;
		if(Phead->data == data || Phead->next = Head->next)
		{
			break;
		}
	}
	if(Phead->next == Head->next) return false;
	Temp = Phead->next;
	Phead->next->next->prev = Phead;
	Phead->next = Phead->next->next;
	Phead->next->next =NULL;
	Phead->next->prev = NULL:
	free(Temp);
	return true;
}
posted @ 2026-08-20 23:19  9Tired  阅读(0)  评论(0)    收藏  举报