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

双向循环链表的接口

头文件

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

创建链表、节点

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

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

} DoubleCircLList_t;

/********************************************************************
 *
 *	name	 :	DoubleCircLList_HeadInsert
 *	function :  创建一个空双向循环链表,空链表应该有一个头结点,对链表进行初始化
 *	argument :
 *				@data:节点需存储的数据
 *	retval	 :  返回新创建链表头节点的地址
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-24
 * 	note	 :	None
 *
 * *****************************************************************/

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

	// 2.对头结点进行初始化,头结点是不存储数据域,指针域指向自身即可,体现“循环”
	Head->prev = Head;
	Head->next = Head;

	// 3.把头结点的地址返回即可
	return Head;
}
/********************************************************************
 *
 *	name	 :	DoubleCircLList_HeadInsert
 *	function :  创建新的结点,并对新结点进行初始化(数据域 + 指针域)
 *	argument :
 *				@data:节点需存储的数据
 *	retval	 :  返回新创建节点的地址
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-24
 * 	note	 :	None
 *
 * *****************************************************************/

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

	// 2.对新结点的数据域和指针域(2个)进行初始化,指针域指向结点自身,体现“循环”
	New->data = data;
	New->prev = New;
	New->next = New;

	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 = DoubleLList_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)
{
	DoubleLList_t *Phead = Head->next;
	// 1.判断链表是否为空,如果为空
	if (NULL == Head->next)
	{
		perror("Linked lists have no data\n");
		return false;
	}

	// 备份首节点地址

	// 链表只有一个节点
	if (NULL == Phead->next)
	{
		free(Phead);
		Head->next = NULL;
		return true;
	}
	// 删除
	Head->next = Phead->next;
	Phead->next->prev = NULL;
	Phead->next = 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)
{
	// 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	 :	DoubleCircLList_Del
 *	function :  中删
 *	argument :	@head:目标链表
 *				@data:需删除的数据
 *
 *	retval	 :  返回1成功0失/********************************************************************
 *
 *	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);
		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);
		return true;
	}
	Phead->prev->next = Phead->next;
	Phead->next->prev = Phead->prev;
	Phead->prev = NULL;
	Phead->next = NULL;
	free(Phead);
	return true;
}败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-24
 * 	note	 :
 *
 * *****************************************************************/
bool DoubleCircLList_Del(DoubleCircLList_t *Head, DataType_t data)
{
	// 备份首节点地址
	DoubleCircLList_t *Phead = Head;
	// 备份头节点地址
	DoubleCircLList_t *Temp = Head->next;
	// 1.判断链表是否为空,如果为空
	if (Head == Head->next)
	{
		perror("Linked lists have no data\n");
		return false;
	}
	// 删除的节点在头
	if (data == Head->next->data)
	{
		DoubleCircLList_HeadDel(Head);
		return true;
	}
	// 遍历找的目标数据节点
	DoubleCircLList_t *P = NULL; // 定义变量保存找的目标数据的节点地址
	while (Head->next != Temp->next)
	{
		Temp = Temp->next;
		if (data == Temp->data)
		{
			P = Temp;
			break;
		}
	}
	// 找不到目标数据
	if (NULL == P)
	{
		perror("The target data cannot be found\n");
		return false;
	}
	// 目标数据在尾部
	if (Head->next == P->next)
	{
		DoubleCircLList_TailDel(Head);
	}
	else
	{
		P->next->prev = P->prev; // 删除节点的后继指向删除节点的前驱
		P->prev->next = P->next; // 删除节点的前驱指向删除节点的后继
		P->next = NULL;			 // 删除节点的首指针释放
		P->prev = NULL;			 // 删除节点的尾指针释放
		free(P);				 // 释放删除节点的堆内存
	}
	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);
	}
}

验证

int main(int argc, char const *argv[])
{
	// 1.创建顺序表
	DoubleLList_t *Manager = DoubleLList_Create();

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

	// 头删除
	DoubleLList_HeadDel(Manager);
	// 遍历顺序表
	DoubleLList_Print(Manager); // --0 8 0 7 9 5 2 1 4 6
	printf("\n");
	// 尾删除
	DoubleLList_TailDel(Manager);
	// 遍历顺序表
	DoubleLList_Print(Manager); // --0 8 0 7 9 5 2 1 4
	printf("\n");
	// 删除顺序表的元素
	DoubleLList_Del(Manager, 2);
	// 遍历顺序表
	DoubleLList_Print(Manager); // --0 8 7 9 5 2 1 4
	printf("\n");
	return 0;
}

输出结果

data = 5
data = 2
data = 1
data = 4
data = 6

data = 10
data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 2
data = 1
data = 4
data = 6

data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 2
data = 1
data = 4
data = 6

data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 2
data = 1
data = 4

data = 0
data = 8
data = 0
data = 7
data = 9
data = 5
data = 1
data = 4
posted @ 2024-04-24 03:45  谁TM买小米啊  阅读(34)  评论(0编辑  收藏  举报