单向循环链表的接口设计

/********************************************************************************************************
*
*
* 设计单向循环链表的接口
*
* 
*
* Copyright (c)  2023-2024   cececlmx@126.com   All right Reserved
* ******************************************************************************************************/
#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_creat(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 = New;

	return New;
}

//头插
bool CircLList_HeadInsert(CircLList_t *Head,DataType_t data)
{
	
	CircLList_t *Phead=Head->next;
	CircLList_t *New = CircLList_NewNode(data);
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

	while(Phead->next!=Head->next)
	{
		Phead=Phead->next;
	}
	New->next = Head->next;
	Phead->next=New;
	Head->next = New;
}

//尾插
bool CircLList_TailInsert(CircLList_t *Head,DataType_t data)
{
	CircLList_t *Phead = Head->next;
	CircLList_t *Phead_prev = Head;
	CircLList_t *New = CircLList_NewNode(data);
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

	if(Phead==Head)
	{
		Head->next=New;
		return true;
	}

	while(Phead->next!=Head->next)
	{
		Phead = Phead->next;
		Phead_prev = Phead_prev->next;
	}

	New->next = Phead->next;
	Phead->next = New;
}


//指定位置插入
bool CircLList_DestInsert(CircLList_t *Head,DataType_t destval,DataType_t data)
{

	CircLList_t *Phead = Head->next;
	CircLList_t *Phead_next = Phead->next;
	CircLList_t *New = CircLList_NewNode(data);
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

	if(Head->next == Head)
	{
		printf("链表为空\n");
		return false;
	}

	while(Phead->data != destval)
	{
		if(Phead->next == Head->next)
		{
			printf("未找到指定数据\n");

			return false;
		}
		Phead = Phead->next;
		Phead_next = Phead_next->next;
	}

	New->next = Phead_next;
	Phead->next = New;
}




//头删
bool CircLList_HeadDel(CircLList_t *Head)
{
	//对单向循环链表的头结点的地址进行备份
	CircLList_t *Phead=Head->next;
	CircLList_t *First=Head->next;
	if(Head->next == Head)
	{
		printf("链表为空\n");
	}
	if(First->next==First)
	{
		printf("1111\n");
		Head->next =Head;
		return true;
	}

	while(Phead->next!=Head->next)
	{
		Phead=Phead->next;
	}

	Head->next=First->next;
	
	Phead->next=Head->next;
	First->next=NULL;
	free(First);
}

//指定删除
bool CircLList_DestDel(CircLList_t *Head, DataType_t dest)
{
    CircLList_t *Phead = Head->next; // 操作指针 初始为指向首结点, 若为空链表则指向头结点
    CircLList_t *Phead_prev = Head;          // 操作指针 存放当前操作指针的前一个结点地址

    // 1.判断单向循环链表是否为空,如果为空,则报错
    if (Head == Head->next)
    {
        printf("Error,  CircularLinkList is empty! \n");
        return false;
    }

    // 2.若单向循环链表非空
    // 目标结点是首结点, 不再继续查找. 目标结点非首结点, 继续向下查找
    while (Phead->data != dest)
    {
        Phead_prev = Phead;                                        // 备份Phead的前一个位置
        Phead = Phead->next;                                      // 进入下一个结点
        if ((Phead->next == Head->next) && (Phead->data != dest)) // 达到末尾 且 末尾不是目标
        {
            printf("The target doesn't exist! \n");
            return false;
        }
    } 
    // 目标结点是首结点
    if ( Phead == Head->next)
    {
        while (Phead_prev->next) // 不断向下检查结点指针域
        {
            Phead_prev = Phead_prev->next;            // 进入下一个结点
            if (Phead_prev->next == Head->next) // 结束条件: 达尾结点
            {
                break;
            }
        }                           // Prev到达未尾结点
        Phead_prev->next = Phead->next; // 更新尾结点指针域为新首结点地址
        Head->next = Phead->next; // 更新首结点链接新首结点

		if(Phead->next==Phead)//是否是单元素判断
		{
			Head->next=Head;
		}
    }
    else if (Phead->next == Head->next) // 目标结点是尾结点
    {
        Phead_prev->next = Head->next; // 新尾结点链接首结点, 行成循环
    }
    else // 目标结点是中间结点
    {
        Phead_prev->next = Phead->next;
    }


	Phead->next = NULL;
    free(Phead);
    return true;
}

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

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

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

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

	return true;
}

bool CircLList_TailDel(CircLList_t *Head)
{
    CircLList_t *Phead = Head->next; // 操作指针 初始为指向首结点, 若为空链表则指向头结点
    CircLList_t *Phead_prev = Head;          // 操作指针 存放当前操作指针的前一个结点地址

    // 1.判断单向循环链表是否为空,如果为空,则报错
    if (Head == Head->next)
    {
        printf("Error,  CircularLinkList is empty! \n");
        return false;
    }

    // 2.若单向循环链表非空
    // 若链表只有首结点
    if (Phead->next == Head->next)
    {
        Head->next = Head; // 空链表状态
        Phead->next = NULL;
        free(Phead); // 防止内存泄漏
        return true;
    }
    // 若还有别的结点
    while (Phead->next) // 不断向下检查结点指针域
    {
        Phead_prev = Phead;
        Phead = Phead->next;         // 进入下一个结点
        if (Phead->next == Head->next) // 结束条件: 达尾结点
        {
            break;
        }
    }                        // Phead到达未尾结点 Prev为Phead 的前一个位置
    Phead_prev->next = Head->next; // 新尾结点链接首结点, 行成循环
    Phead->next = NULL;
    free(Phead); // 防止内存泄漏
    return true;
}

int main(int argc, char const *argv[])
{

	//1.创建顺序表
	CircLList_t * Manager = CircLList_creat();
	
	//2.向顺序表中的尾部插入新元素
	printf("*********************************尾插********************************\n");
	CircLList_TailInsert(Manager,5);
	CircLList_TailInsert(Manager,2);
	CircLList_TailInsert(Manager,1);
	CircLList_TailInsert(Manager,4);
	CircLList_TailInsert(Manager,6);  


	// //3.遍历顺序表
	CircLList_Print(Manager); // -- 5 2 1 4 6
	printf("\n");
	//4.向顺序表中的头部插入新元素
	printf("*********************************头插********************************\n");
	CircLList_HeadInsert(Manager,9);
	CircLList_HeadInsert(Manager,7);
	CircLList_HeadInsert(Manager,8);
	CircLList_HeadInsert(Manager,0);
	CircLList_HeadInsert(Manager,10);  

	// //5.遍历顺序表
	CircLList_Print(Manager); // --10 0 8 7 9 5 2 1 4 6
	printf("\n");
	//6.删除顺序表的元素
	printf("*********************************头删********************************\n");
	CircLList_HeadDel(Manager);
	CircLList_HeadDel(Manager);
	CircLList_HeadDel(Manager);
	CircLList_HeadDel(Manager);
	CircLList_HeadDel(Manager);


	CircLList_Print(Manager);// --5  2  1  4  6 


	//7.指定插入
	printf("*********************************指定插入********************************\n");
	CircLList_DestInsert(Manager,5,1);
	CircLList_DestInsert(Manager,2,3);
	CircLList_DestInsert(Manager,6,7);

	//8.遍历顺序表
	CircLList_Print(Manager); // --5 1 2 3 1 4 6 7
	printf("\n");

	//9.指定删除
	printf("*********************************指定删除********************************\n");
	CircLList_DestDel(Manager,5);
	CircLList_DestDel(Manager,7);
	CircLList_DestDel(Manager,3);

	//10.遍历顺序表
	CircLList_Print(Manager); // -- 1 2 1 4 6 
	printf("\n");

	//11.尾部删除
	printf("*********************************尾删********************************\n");
	CircLList_TailDel(Manager);
	CircLList_TailDel(Manager);
	CircLList_TailDel(Manager);
	CircLList_TailDel(Manager);

	//12.遍历顺序表
	CircLList_Print(Manager); // --1  
	printf("\n");
}
posted @ 2024-04-24 00:48  藍桉未遇釋槐鳥  阅读(14)  评论(0)    收藏  举报