首页 > 其他分享 >C语言单向循环链表的增删操作

C语言单向循环链表的增删操作

时间:2024-04-24 09:14:28浏览次数:16  
标签:结点 Head CircLList next 链表 增删 New 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 (NULL == Head->next)
	{
		Head->next = New;
        New->next = New;
		return true;
	}

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

	// 4.把新结点插入到链表的头部
    New->next = Head->next;
    Head->next = New;
    Last->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 (NULL == 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 (NULL == 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 (NULL == Head->next)
	{
		return false;
	}

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

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

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

	return true;
}

// 尾删
bool CircLList_TailDel(CircLList_t *Head)
{
    // 1.判断链表是否为空,如果为空,则直接退出
	if (NULL == 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 (NULL == 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;
}

标签:结点,Head,CircLList,next,链表,增删,New,C语言
From: https://www.cnblogs.com/beborn00/p/18154303

相关文章

  • 删除单链表中最小值的结点
    /***********************************************filename:demo14.c*author:[email protected]*date:2024/4/2*function:设计1个函数,实现删除单链表中最小值的结点*note:None*CopyRight(c)2023-2024邮箱AllRightReseverd***********************************......
  • 数据结构:双向链表的创建·插入·删除
    数据结构:双向链表的创建·插入·删除/***@filename:数据结构:双向链表的创建·插入·删除*@brief:实现双向链表的创建·插入·删除*@author :[email protected]*@date :2024/04/23*@version:1.0*@note:none*CopyRight(c......
  • 双向链表(不循环)
    双向链表双向链表的原理与应用如果想要提高单向链表或者单向循环链表的访问速度,则可以在链表中的结点中再添加一个指针域,让新添加的指针域指向当前结点的直接前驱的地址,也就意味着一个结点中有两个指针域(prev+next),也被称为双向链表(DoubleLinkedList)。单向循环链表实现分析......
  • 双向不循环链表
    双向不循环链表/***********************************************************************************************************设计双向链表的接口****Copyright(c)[email protected]*******************************......
  • 单项链表的一些基础操作
    /***********************************************filename:LinkList.c*author:[email protected]:2024/4/2function:设计顺序表note:NoneCopyRight(c)2023-2024邮箱AllRightReseverd///指的是单向链表中的结点有效数据类型,用户可以根据需要进行修改type......
  • 单项循环链表的一些基本操作
    //设计单向循环列表/***********************************************filename:circularlinkedlist.c*author:[email protected]*date:2024/4/23*function:设计单向循环列表*note:None*CopyRight(c)2023-2024邮箱AllRightReseverd**************************......
  • 双向循环链表的一些基础操作
    /***********************************************filename:DoubleList.c*function:设计双向链表*author:[email protected]*date:2024/4/23*note:None*CopyRight(c)2023-2024邮箱AllRightReseverd***********************************************///指的是......
  • 数据结构笔试题——基于C语言的链表功能函数实现
    题目1题目要求如下:/***@functionname:LList_CntdmFind*@brief查找链表中,倒数第k个位置上的节点*@param:​ @Head:链表头节点​ @k :倒数第k个位置*@retval:int型返回值;返回-1时即为失败,返回0时表示成功;*@date:2024/04/23*@version1.0*@n......
  • 单向与双向循环链表
    单向循环链表/********************************************************************* 函数名称: *函数功能:设计单向循环链表的接口*函数参数:* ​*返回结果:*注意事项:None*函数作者:zcx982795194@[email protected]*创建......
  • C语言单向循环链表的增删操作
    /***********************************************************************************************************设计双向链表的接口****Copyright(c) 2023-2024 [email protected] AllrightReserved****************************************......