设计单向循环链表的插入、删除和遍历接口

/**********************************************************************************
*

  •      file name:  003_单向循环链表.c
    
  •      author   :  A13326981379@163.com
    
  •      date     :  2024/04/23
    
  •      function :  设计单向循环链表的接口
    
  •      note     :  None
    
  •      CopyRight (c) 2024-2024  A13326981379@163.com    All Right Reseverd
    
  • *******************************************************************************/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>


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

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

}CircLList_t;


//创建一个空单向循环链表,空链表应该有一个头结点,对链表进行初始化
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;

	//3.把头结点的地址返回即可
	return Head;
}

//创建新的结点,并对新结点进行初始化(数据域 + 指针域)
CircLList_t * CircLList_NewNode(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;
	}

	//2.对新结点的数据域和指针域进行初始化
	New->data = data;
	New->next = NULL;

	return New;
}

//头插
bool CircLList_HeadInsert(CircLList_t *head,DataType_t data)
{
    //将传进来的数据创建一个新的节点
    CircLList_t *New = CircLList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
		return false;
    }
    //定义一个变量存储尾节点
    CircLList_t *tmp = head->next;
    //判断是否为空链表
    
    if (head->next == head)
    {
        head->next = New;
        New->next = New;
        return true;
    }
    //遍历到尾节点
    while (tmp->next != head->next)
    {
        tmp = tmp->next;
    }
    //将新节点插入头部
    New->next = head->next;
    tmp->next = New;
    
    head->next = New;
    return true;
    
}

//尾插
bool CircLList_TailInsert(CircLList_t *head,DataType_t data)
{
    //将传进来的数据创建一个新的节点
	CircLList_t *New = CircLList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
		return false;
    }
    CircLList_t *tmp = head->next;
    //判断是否为空链表
    if (head->next == head)
    {
        head->next = New;
        New->next = New;
        return true;
    }
    //遍历到尾节点
    while (tmp->next != head->next)
    {
        tmp = tmp->next;
    }
    tmp->next = New;
    New->next = head->next;
    return true;
    
    
    
    

}


//指定位置插入(找链表中已存在的数据的后面插)
bool CircLList_DestInsert(CircLList_t *head,DataType_t destval,DataType_t data)
{
	//将传进来的数据创建一个新的节点
	CircLList_t *New = CircLList_NewNode(data);
    if (NULL == New)
    {
        printf("can not insert new node\n");
		return false;
    }
    //判断是否为空链表
    if (head->next == head)
    {
        head->next = New;
        New->next = New;
        return true;
    }
    //定义两个变量记录头节点和首节点地址
    CircLList_t *tmp1 = head;
    CircLList_t *tmp2 = head->next;
    while (tmp2->next != head->next)
    {
        tmp2 = tmp2->next;
        tmp1 = tmp1->next;
        if (tmp2->data == destval)
        {  
            //指定位置刚好在尾部(后插)
            if (tmp2->next == head->next)
            {
                New->next = tmp2->next;
                tmp2->next = New;
                New->next = head->next;
                return true;
            }
            New->next = tmp2->next;
            tmp2->next = New;
        }
        else if (tmp1->data == destval)
        {
            //指定位置刚好在头部(后插)
            if (tmp1 == head->next)
            {
                New->next = tmp1->next;
                tmp1->next = New;
                return true;
            }
        }
    }
    return true;
}


//遍历链表
bool CircLList_Print(CircLList_t *Head)
{
	//对单向循环链表的头结点的地址进行备份
	CircLList_t *Phead = Head;
	
	//判断当前链表是否为空,为空则直接退出
	if (Head->next == Head)
	{
		printf("current linkeflist is empty!\n");
		return false;
	}

	//从首结点开始遍历
	while(Phead->next)
	{
		//把头结点的直接后继作为新的头结点
		Phead = Phead->next;

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

		//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
		if (Phead->next == Head->next)
		{
			break;
		}	
	}

	return true;
}
//头删
bool LList_HeadDel(CircLList_t *Head)
{
	//对单向循环链表的头结点的地址进行备份
	CircLList_t *Phead = Head->next;
    CircLList_t *tmp = Head->next;
    //链表为空的情况下
    if (Head->next == Head)
    {
        printf("此链表为空!没有元素可以删除!");
        return false;
    }
    //遍历尾节点
    while(tmp->next != Head->next)
    {
        tmp = tmp->next;
    }
    //只有一个节点的情况下
    if(Phead->next == Phead)
    {
        Head->next = Head;
        Phead->next = NULL;
        free(Phead);
        return true;
    }
    Head->next = Phead->next;
    tmp->next = Phead->next;
    Phead->next = NULL;
    free(Phead);
    return true;
}
//尾删
bool LList_TailDel(CircLList_t *Head)
{
    //对单向循环链表的头结点的地址进行备份
    CircLList_t *Phead = Head;
    CircLList_t *tmp = Head->next;
    //链表为空的情况下
    if (Head->next == Head)
    {
        printf("此链表为空!没有元素可以删除!");
        return false;
    }
//只有一个节点的情况下
    if(tmp->next == tmp)
    {
        Head->next = Head;
        tmp->next = NULL;
        free(tmp);
        return true;
    }
    //遍历尾节点
    while(tmp->next != Head->next)
    {
        Phead = Phead->next;
        tmp = tmp->next;
    }
    Phead->next = tmp->next;
    tmp->next = NULL;
    free(tmp);
    return true;
}
//指定删
bool CircLList_DestDel(CircLList_t *Head,DataType_t destval)
{
    //链表为空的情况下
    if (Head->next == Head)
    {
        printf("此链表为空!没有元素可以删除!");
        return false;
    }
    CircLList_t *tmp1 = Head;
    CircLList_t *tmp2 = Head->next;
    do{
        while(tmp2->data == destval)
        {  
            //指定位置刚好在头部
            if(tmp2 == Head->next)
            {
                
                CircLList_t *Phead = Head->next;
                // 遍历到尾节点
                while (Phead->next != Head->next)
                {
                    Phead = Phead->next;
                }
                //链表只有一个的情况下
                if (tmp2->next == tmp2)
                {
                    Head->next = Head;
                    tmp2->next = NULL;
                    free(tmp2);
                    return true;
                }
                Head->next = tmp2->next;
                Phead->next = tmp2->next;
                tmp2->next = NULL;
                free(tmp2);
                return true;
            }
            //在中间
            tmp1->next = tmp2->next;
            tmp2->next = NULL;
            free(tmp2);
            return true;
            //指定位置刚好在尾部
            if (tmp2->next == Head->next)
            {
                printf("cc\n");
                tmp1->next = tmp2->next;
                tmp2->next = NULL;
                free(tmp2);
                return true;
            }
        }
        tmp2 = tmp2->next;
        tmp1 = tmp1->next;
    }while(tmp1->next != Head->next);
    printf("链表中没有你需要删除的数!\n"); 
    return false;
}




int main(int argc, char const *argv[])
{
	CircLList_t *head = CircLList_Create();
    //头插入
    CircLList_HeadInsert(head,5);
    CircLList_HeadInsert(head,6);
    CircLList_HeadInsert(head,7);
    CircLList_HeadInsert(head,8);
    CircLList_HeadInsert(head,9);
    CircLList_Print(head);

    //尾插
    printf("尾插法:\n");
    CircLList_TailInsert(head,55);
    CircLList_Print(head);
    //指定位置插入(找链表中已存在的数据的后面插)
    printf("指定插:\n");
    CircLList_DestInsert(head,9,99);
    CircLList_DestInsert(head,6,909);
    CircLList_DestInsert(head,55,9900);
    CircLList_Print(head);
    // LList_HeadDel(head);
    // LList_TailDel(head);
    printf("\n");
    CircLList_DestDel(head,9900);

    CircLList_DestDel(head,91);
    
    CircLList_Print(head);
	return 0;
}
posted @   A-A-A-Ariana  阅读(10)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示