C语言数据结构:单向循环链表的增删操作

/********************************************************************************************************
*
*
* 设计单向循环链表的接口
*
* 
*
* Copyright (c)  2023-2024   [email protected]   All right Reserved
* ******************************************************************************************************/


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

//构造单向循环链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct CircularLinkedList
{
	DataType_t  		 data; //结点的数据域
	struct LinkedList	*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)
{
	// 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;
        New->next = New;
		return true;
	}

    // 3.如果单向循环链表为非空,遍历找到尾结点
    CircLList_t *Last = Head;
    while(Last->next != Head->next)
    {
        Last = Last->next;
    }

	// 4.把新结点插入到链表的头部
	Last->next = New;
    New->next = Head->next;
    Head->next = New;

    return true;
}

//尾插
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;
        New->next = New;
		return true;
	}

    // 3.如果单向循环链表为非空,遍历找到尾结点
    CircLList_t *Last = Head;
    while(Last->next != Head->next)
    {
        Last = Last->next;
    }

    // 4.把新结点插入到链表的尾部
    Last->next = New;
    New->next = Head->next;

    return true;
}


//指定位置插入
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;
        New->next = New;
		return true;
	}

    // 3.遍历链表,比较结点的数据域,找到目标结点
	CircLList_t *Dest = Head->next;
	while (Dest->data != destval && Dest != NULL)
	{
		Dest = Dest->next;
	}
	if (NULL == Dest)
	{
		return false;
	}

	// 4.说明找到目标结点,则把新结点插入到目标结点的后面
	New->next = Dest->next;
	Dest->next = New;

    return true;
}

// 头删
bool CircLList_HeadDel(CircLList_t *Head)
{
	// 1.判断链表是否为空,如果为空,则直接退出
	if (Head == Head->next)
	{
		return false;
	}

    // 2.对链表的首结点的地址进行备份
	CircLList_t *Temp = Head->next;

	// 3.链表是非空的,判断是否只有首结点
	if(Head->next == Head->next->next)
	{
		Temp->next = NULL;
		Head->next = Head;
		free(Temp);
		return true;
	}

	// 4.遍历链表,找到尾结点
    CircLList_t *Last = Head;
    while(Last->next != Head->next)
    {
        Last = Last->next;
    }

    // 5.删除首结点
	Last->next = Temp->next;
    Head->next = Temp->next;
	Temp->next = NULL;
	free(Temp);

	return true;
}

// 尾删
bool CircLList_TailDel(CircLList_t *Head)
{
    // 1.判断链表是否为空,如果为空,则直接退出
	if (Head == Head->next)
	{
		return false;
	}

    // 2.链表是非空的,遍历链表,找到尾结点以及尾结点的直接前驱
    CircLList_t *Last_pre = Head;
    CircLList_t *Last = Head->next;
    while(Last->next != Head->next)
    {
        Last = Last->next;
        Last_pre = Last_pre->next;
    }

    // 3.删除尾结点
    Last_pre->next = Head->next;
    Last->next = NULL;
    free(Last);

	return true;
}

// 指定删
bool CircLList_DestDel(CircLList_t *Head, DataType_t destval, DataType_t data)
{
    // 1.判断链表是否为空,如果为空,则直接退出
	if (Head == Head->next)
	{
		return false;
	}

    // 2.链表是非空的,遍历链表,找到待删除结点以及待删除结点的直接前驱
	CircLList_t *Dest_pre = Head;
    CircLList_t *Dest = Head->next;
    while (Dest->data != destval && Dest != NULL)
	{
		Dest = Dest->next;
        Dest_pre = Dest_pre->next;
	}
	if (NULL == Dest)
	{
		return false;
	}

    // 3.删除指定结点
	Dest_pre->next = Dest->next;
    Dest->next = NULL;
	free(Dest);

	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;
}

int main(int argc, char const *argv[])
{
	
	return 0;
}

热门相关:北宋大丈夫   帝少夜宠:小甜妻,乖!   恶魔总裁霸道宠:老婆,甜似火   剑斗九天   我有无数神剑