单向循环链表的增和删

单向循环链表的增和删

/**
  * @file name:	
  * @brief       :创建单向循环链表实现插入结点和删除结点
  * @author      :qp686868@126.com
  * @date        :2024/04/23
  * @version 1.0 :1.0
  * @note        :none
  * CopyRight (c)  2023-2024   qp686868@126.com   All Right Reseverd
  */

 //指的是单向循环链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int  DataType_t;

//构造链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct CircularLinkedList
{
	DataType_t  		                         data; //结点的数据域
	struct typedef struct CircularLinkedList	*next; //结点的指针域

}ircLList_t;



//创建一个空单向循环链表,空链表应该有一个头结点,对链表进行初始化
/**
  * @function name:	  :CircLList_Create
  * @brief            :创建一个空单向循环链表,空链表应该有一个头结点,对链表进行初始化
  * @param            : void
  * @retval 
  * @date             :2024/04/23
  * @version 1.0 :   :1.o 
  * @note            : none
  */
CircLList_t * CircLList_Create(void)
{
    ////1.创建一个头结点并对头结点申请内存
	CircLList_t *Head = (CircLList_t *)calloc(1,sizeof(CircLList_t));
	if (NULL == Head)
	{
		perror("Calloc memory for Head is Failed");
		exit(-1);
	}
    //2.对头结点进行初始化,头结点是不存储数据域,指针域指向自身,体现“循环”思想
    Head->next=Head;
    return Head;
}




//创建新的结点,并对新结点进行初始化(数据域 + 指针域)
/**
  * @function name:	  :CircLList_Create
  * @brief            :创建新的结点,并对新结点进行初始化(数据域 + 指针域)
  * @param            : data
  * @retval           :返回这个新结点的地址
  * @date             :2024/04/23
  * @version 1.0 :   :1.o 
  * @note            : none
  */
  CircLList_t * CircLList_Create(DataType_t data)
  {
    //1.创建一个新结点并对新结点申请内存
	CircLList_t *New = (CircLList_t *)calloc(1,sizeof(CircLList_t));
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}
    New->next=NULL;
    New->data=data;
    return New;
  }




//头插
/**
  * @function name:	  :CircLList_HeadInsert
  * @brief            :在这个单向循环链表头部插入结点
  * @param            : *Head data  
  * @retval           :返回插入是否成功
  * @date             :2024/04/23
  * @version 1.0 :   :1.o 
  * @note            : none
  */
bool CircLList_HeadInsert(CircLList_t *Head,DataType_t data)
{
    //1.创建新的结点,并对新结点进行初始化
	CircLList_t *New = CircLList_NewNode(data);
    if(NULL == New){
        printf("can not insert new node\n");
		return false;
    }
     //2.判断链表是否为空,如果为空,则直接插入即可
	if (Head== Head->next)
	{
		Head->next = New;
		return true;
    }
    CircLList_t *Phead=Head;
    //遍历查找尾结点
    while(Phead->next!=Head->next){
        Phead=Phead->next;
    }
    New->next=Phead->next;
    Head->next=New;
    Phead->next=New;
    return true;
}



//尾插
/**
  * @function name:	  :CircLList_TailInsert
  * @brief            :在这个单向循环链表尾部插入结点
  * @param            : *Head data  
  * @retval           :返回插入是否成功
  * @date             :2024/04/23
  * @version 1.0 :   :1.o 
  * @note            : none
  */
bool CircLList_TailInsert(CircLList_t *Head,DataType_t data)
{
    //1.创建新的结点,并对新结点进行初始化
	CircLList_t *New = CircLList_NewNode(data);
    if(NULL == New){
        printf("can not insert new node\n");
		return false;
    }
     //2.判断链表是否为空,如果为空,则直接插入即可
	if (Head== Head->next)
	{
		Head->next = New;
		return true;
    }
     CircLList_t *Phead=Head;
    //遍历查找尾结点
    while(Phead->next!=Head->next){
        Phead=Phead->next;
    }
    Phead->next=New;
    New->next=Head->next;
    return true;
}
	
  





//指定位置插入
/**
  * @function name:	  :CircLList_DestInsert
  * @brief            :在这个单向循环链表指定位置插入结点
  * @param            : *Head data  destval
  * @retval           :返回插入是否成功
  * @date             :2024/04/23
  * @version 1.0      :1.o 
  * @note            : none
  */
bool CircLList_DestInsert(CircLList_t *Head,DataType_t destval,DataType_t data)
{
	//1.创建新的结点,并对新结点进行初始化
	CircLList_t *New = CircLList_NewNode(data);
    if(NULL == New){
        printf("can not insert new node\n");
			return false;
    }
     //2.判断链表是否为空,如果为空,则直接插入即可
	if (Head== Head->next)
	{
		Head->next = New;
		return true;
    }
        CircLList_t *Phead=Head;
    while(Phead->data!=destval){
        Phead=Phead->next;
    }
      New->next=Phead->next;
      Phead->next=New;
      return true;
}




/**
  * @function name:	  :CircLList_HeadDel
  * @brief            :在这个单向循环链表的头部删除一个结点
  * @param            : *Head  
  * @retval           :返回插入是否成功
  * @date             :2024/04/23
  * @version 1.0 :   :1.o 
  * @note            : none
  */
bool CircLList_HeadDel(CircLList_t *Head)
{
    //判断单向循环列表是否为空
     if(Head== Head->next){
			return false;
    }
     CircLList_t *Phead1=Head;
     CircLList_t *Phead2=Head->next;
    while(Phead1->next!=Head->next){
        Phead1=Phead->next;
    }
    Phead1->next=Head->next->next;
    Head=Head->next->next;
    Phead2->next=NULL;
    free(Phead);
    return true;
}




/**
  * @function name:	  :CircLList_TailDel
  * @brief            :在这个单向循环链表的尾部删除一个结点
  * @param            : *Head  
  * @retval           :返回插入是否成功
  * @date             :2024/04/23
  * @version 1.0 :   :1.o 
  * @note            : none
  */
bool CircLList_TailDel(CircLList_t *Head)
{
     //判断单向循环列表是否为空
     if(Head->next==Head){
			return false;
    }
     CircLList_t *Phead=Head;
     CircLList_t *prev;
    while(Phead->next!=Head->next){
        prev=Phead;
        Phead=Phead->next;
    }
    prev->next=Head->next;
    phead->next=NULL;
    free(Phead);
    return true;
}



/**
  * @function name:	  :CircLList_Create
  * @brief            :在这个单向循环链表的中部删除一个结点
  * @param            : *Head  
  * @retval           :返回插入是否成功
  * @date             :2024/04/23
  * @version 1.0 :   :1.o 
  * @note            : none
  */
  bool CircLList_MediDel(CircLList_t *Head,DataType_t destval,DataType_t data)
  {
    //判断单向循环列表是否为空
     if(Head->next==Head){
			return false;
    }
    }
     CircLList_t *Phead=Head;
     CircLList_t *prev;
    if(Head->next==Head->next){
        Head->next->next=NULL;
        Head->next=NULL;
        free(Head->next);
    while(Phead->data!=destval||Phead->next!=Head->next){
        prev=Phead;
        Phead=Phead->next;
    }
  }
  //遍历
void CircLList_Print(LList_t *Head)
{
	//对链表的头文件的地址进行备份
	CircLList_t *Phead = Head;
	
	//首结点
	while(Phead->next!=Head)
	{
		//把头的直接后继作为新的头结点
		Phead = Phead->next;

		//输出头结点的直接后继的数据域
		printf("data = %d\n",Phead->data);
	}

}
int main()
{
    return 0;
}

posted @ 2024-05-14 20:01  红枫叶$$  阅读(24)  评论(0)    收藏  举报