双向链表的接口的接口程序
  6Df3JnWQUC5m 11天前 16 0

双向链表的接口的接口程序

/*******************************************************************
 *
 *	file name:	双向链表的接口的接口程序
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-23
 *	function :
 * 	note	 :  None
 *
 *	CopyRight (c)  2024   17647576169@163.com   All Right Reseverd
 *
 * *****************************************************************/

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

// 构造双向链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct DoubleLinkedList
{
	DataType_t data;			   // 结点的数据域
	struct DoubleLinkedList *prev; // 直接前驱的指针域
	struct DoubleLinkedList *next; // 直接后继的指针域

} DoubleLList_t;

/********************************************************************
 *
 *	name	 :	DoubleLList_Create
 *	function :  创建一个空双向链表,空链表应该有一个头结点,对链表进行初始化
 *	argument :	None
 *
 *	retval	 :  返回创建的链表的头地址
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-23
 * 	note	 :
 *
 * *****************************************************************/

DoubleLList_t *DoubleLList_Create(void)
{
	// 1.创建一个头结点并对头结点申请内存
	DoubleLList_t *Head = (DoubleLList_t *)calloc(1, sizeof(DoubleLList_t));
	if (NULL == Head)
	{
		perror("Calloc memory for Head is Failed");
		exit(-1);
	}

	// 2.对头结点进行初始化,头结点是不存储数据域,指针域指向NULL
	Head->prev = NULL;
	Head->next = NULL;

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

/********************************************************************
 *
 *	name	 :	CircLList_NewNode
 *	function :  创建新的结点,并对新结点进行初始化(数据域 + 指针域)
 *	argument :	@data:需插入的数据
 *
 *	retval	 :  返回新创建链表节点的地址
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-23
 * 	note	 :
 *
 * *****************************************************************/

DoubleLList_t *DoubleLList_NewNode(DataType_t data)
{
	// 1.创建一个新结点并对新结点申请内存
	DoubleLList_t *New = (DoubleLList_t *)calloc(1, sizeof(DoubleLList_t));
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

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

	return New;
}

/********************************************************************
 *
 *	name	 :	DoubleLList_HeadInsert
 *	function :  向链表的头部进行数据插入
 *	argument :	@head:目标链表
 *				@data:需插入的数据
 *	retval	 :  返回新创建链表节点的地址
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool DoubleLList_HeadInsert(DoubleLList_t *Head, DataType_t data)
{
	// 备份头节点
	DoubleLList_t *Phead = Head;
	// 1.创建新的结点,并对新结点进行初始化
	DoubleLList_t *New = DoubleLList_NewNode(data);
	if (NULL == New)
	{
		printf("can not insert new node\n");
		return false;
	}

	// 2.判断链表是否为空,如果为空,则直接插入即可
	if (NULL == Head->next)
	{
		Head->next = New;
		return true;
	}

	// 3.如果链表为非空,则把新结点插入到链表的头部
	New->next = Head->next;
	Head->prev = New;
	Head->next = New;
	return true;
}

/********************************************************************
 *
 *	name	 :	LList_TailInsert
 *	function :  向链表的尾部进行数据插入
 *	argument :	@head:目标链表
 *				@data:需插入的数据
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool DoubleLList_TailInsert(DoubleLList_t *Head, DataType_t data)
{
	// 1.创建新的结点,并对新结点进行初始化
	DoubleLList_t *New = DoubleLList_NewNode(data);
	if (NULL == New)
	{
		printf("can not insert new node\n");
		return false;
	}

	// 2.判断链表是否为空,如果为空,则直接插入即可
	if (NULL == Head->next)
	{
		Head->next = New;
		return true;
	}

	// 3对链表的头文件的地址进行备份
	DoubleLList_t *Phead = Head;
	// 4遍历链表找到尾部
	while (Phead->next)
	{
		// 把头的直接后继作为新的头结点
		Phead = Phead->next;
	}
	// 5.把新结点插入到链表的尾部
	Phead->next = New;
	New->prev = Phead;
	return true;
}

/********************************************************************
 *
 *	name	 :	DoubleLList_DestInsert
 *	function :  向链表的指定位置后进行插入
 *	argument :	@head:目标链表
 *				@data:需插入的数据
 *				@dest:插入位置
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool DoubleLList_DestInsert(DoubleLList_t *Head, DataType_t destval, DataType_t data)
{
	// 1.创建新的结点,并对新结点进行初始化
	DoubleLList_t *New = LList_NewNode(data);

	if (NULL == New)
	{
		printf("can not insert new node\n");
		return false;
	}

	// 2.判断链表是否为空,如果为空
	if (NULL == Head->next)
	{
		return false;
	}
	// 备份头节点地址
	DoubleLList_t *Phead = Head;
	DoubleLList_t *P = NULL;
	// 找目标数据
	while (NULL != Phead->next)
	{
		Phead = Phead->next;
		if (destval == Phead->data)
		{
			P = Phead;
			break;
		}
	}
	// 找不到目标数据
	if (NULL == P)
	{
		perror("The target data cannot be found\n");
		return false;
	}
	// 目标数据在尾部
	if (NULL == Phead->next)
	{
		DoubleLList_TailInsert(Head, data);
		return true;
	}
	// 目标数据在头部
	if (Head->next == Phead)
	{
		DoubleLList_HeadInsert(Head, data);
		return true;
	}
	// 开始插入
	New->next = Phead->next;
	Phead->next->prev = New;
	New->prev = Phead;
	Phead->next = New;
	return true;
}

/********************************************************************
 *
 *	name	 :	LList_DestInsert
 *	function :  头删
 *	argument :	@head:目标链表
 *				@data:需删除的数据
 *
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool DoubleLList_HeadDel(DoubleLList_t *Head, DataType_t data)
{
	// 1.判断链表是否为空,如果为空
	if (NULL == Head->next)
	{
		perror("Linked lists have no data\n");
		return false;
	}

	// 备份首节点地址
	DoubleLList_t *Phead = Head->next;
	// 链表只有一个节点
	if (NULL == Phead->next)
	{
		free(Phead);
		Head->next = NULL;
		return true;
	}
	// 删除
	Head->next = Phead->next;
	Phead->next = NULL;
	Head->next->prev = NULL;
	free(Phead);

	return true;
}
/********************************************************************
 *
 *	name	 :	LList_DestInsert
 *	function :  尾删
 *	argument :	@head:目标链表
 *				@data:需删除的数据
 *
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool DoubleLList_TailDel(DoubleLList_t *Head, DataType_t data)
{
	// 1.判断链表是否为空,如果为空
	if (NULL == Head->next)
	{
		perror("Linked lists have no data\n");
		return false;
	}

	// 备份首节点地址
	DoubleLList_t *Phead = Head->next;
	// 链表只有一个节点
	if (NULL == Phead->next)
	{
		free(Phead);
		Head->next = NULL;
		return true;
	}
	// 遍历找到尾结点
	while (NULL == Phead->next)
	{
		Phead = Phead->next;
	}
	Phead->prev->next = NULL;

	Phead->prev = NULL;
	free(Phead);
	return true;
}
/********************************************************************
 *
 *	name	 :	LList_DestInsert
 *	function :  中删
 *	argument :	@head:目标链表
 *				@data:需删除的数据
 *
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/
bool DoubleLList_Del(DoubleLList_t *Head, DataType_t data)
{
	// 1.判断链表是否为空,如果为空
	if (NULL == Head->next)
	{
		perror("Linked lists have no data\n");
		return false;
	}
	// 删除的节点在头
	if (Head->next->data == data)
	{
		DoubleLList_HeadDel(Head, data);
		return true;
	}
	// 遍历到尾结点
	DoubleLList_t *P = NULL;
	// 备份头节点地址
	DoubleLList_t *Phead = Head;
	while (NULL == Phead->next)
	{
		Phead = Phead->next;
		if (data == Phead->data)
		{
			P = Phead;
			break;
		}
	}

	// 找不到目标数据
	if (NULL == P)
	{
		perror("The target data cannot be found\n");
		return false;
	}
	// 目标数据在尾部
	if (NULL == Phead->next)
	{
		DoubleLList_TailDel(Head, data);
		return true;
	}
	Phead->prev->next = Phead->next;
	Phead->next->prev = Phead->prev;
	Phead->prev = NULL;
	Phead->next = NULL;
	free(Phead);
	return true;
}
/********************************************************************
 *
 *	name	 :	LList_Print
 *	function :  遍历链表
 *	argument :	@head:目标链表
 *
 *
 *	retval	 :  none
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

void DoubleLList_Print(DoubleLList_t *Head)
{
	// 对链表的头文件的地址进行备份
	DoubleLList_t *Phead = Head;

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

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

​```
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 11天前 0

暂无评论

推荐阅读
6Df3JnWQUC5m