双向链表接口设计(C语言)

双向链表接口设计

/**

* @file name: 双向链表接口设计(非循环接口)

* @brief

* @author [email protected]

* @date 2024/04/23

* @version 1.0 :

* @property :

* @note

* CopyRight (c) 2023-2024 [email protected] All Right Reseverd

*/


构造双向循环链表结构体

// 指的是双向链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;
// 构造双向链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct DoubleLinkedList
{
	DataType_t data;			   // 结点的数据域
	struct DoubleLinkedList *prev; // 直接前驱的指针域
	struct DoubleLinkedList *next; // 直接后继的指针域
} DoubleLList_t;


创建一个空双向链表并初始化

DoubleLList_t *DoubleLList_Create()
{
	// 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;
}


创建新的结点,并对新结点进行初始化

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

功能函数:从首节点进行插入元素

bool DoubleLList_HeadInsert(DoubleLList_t *Head, DataType_t data)
{
	DoubleLList_t *new = DoubleLList_NewNode(data);
	DoubleLList_t *tmp = Head->next;
	if (Head->next == NULL) // empty list 链表为空的情况
	{
		Head->next = new;
		return true;
	}
	new->next = Head->next; // normal situation 普通情况
	Head->next->prev = new;
	Head->next = new;
	return true;
}

功能函数:从尾部插入新元素

bool DoubleLList_TailInsert(DoubleLList_t *Head, DataType_t data)
{
	DoubleLList_t *new = DoubleLList_NewNode(data);
	DoubleLList_t *tmp = Head->next;
	if (Head->next == NULL) // judge is the null 链表为空的情况
	{
		Head->next = new;
		return true;
	}
	while (tmp->next != NULL) // as the normal situation,find the last node 普通情况,遍历寻找最后的节点
		tmp = tmp->next;
	tmp->next = new; // 在尾部插入节点
	new->prev = tmp;
	return true;
}


功能函数:从指定位置插入新元素

bool DoubleLList_DestInsert(DoubleLList_t *Head, DataType_t destval, DataType_t data)
{
	DoubleLList_t *tmp = Head->next;
	DataType_t i = Head->data;
	if (Head->next == NULL) // judge the empty list 链表为空的情况
	{
		printf("The list is empty,there is no destival.\n");
		return false;
	}
	DoubleLList_t *new = DoubleLList_NewNode(data);
	while (destval != tmp->data && tmp->next != NULL)
	// as the normal situation,find the destval找到目标值
	{
		tmp = tmp->next;
	}
	if (destval == tmp->data) // 如果存在目标值,则进行插入操作
	{
		new->next = tmp->next;
		tmp->next->prev = new;
		new->prev = tmp;
		tmp->next = new;
		return true;
	}
	else // 如果不存在目标值,则插入无效,直接返回
	{
		printf("There is no destval\n");
		return false;
	}
}

功能函数:删除首节点

bool DoubleLList_HeadDel(DoubleLList_t *Head)
{
	// 对链表的首结点的地址进行备份
	DoubleLList_t *Phead = Head->next;
	// DoubleLList_t *tmp = Head->next;
	if (Head->next == NULL) // 判断当前链表是否为空,为空则直接退出
	{
		printf("current linkeflist is empty!\n");
		return false;
	}
	Head->next = Phead->next; // delete the head 直接进行删除首节点操作
	Phead->next = NULL;
	Phead->prev = NULL;
	Head->next->prev = NULL;
	free(Phead);
	return true;
}

功能函数:删除尾部节点

bool DoubleLList_TailDel(DoubleLList_t *Head)
{
	DoubleLList_t *tmp = Head->next;
	if (Head->next == NULL) // 判断当前链表是否为空,为空则直接退出
	{
		printf("current linkeflist is empty!\n");
		return false;
	}
	while (tmp->next != NULL) // find the last node 普通情况,遍历寻找最后的节点
	{
		tmp = tmp->next;
	}
	tmp->prev->next = NULL; // 此处应考虑,是否需要增加一个变量记录目标值直接前驱
	tmp->prev = NULL;
	tmp->next = NULL;
	free(tmp);
	return true;
}

功能函数:删除指定位置的节点

bool DoubleLList_MidDel(DoubleLList_t *Head, DataType_t destval)
{
	DoubleLList_t *tmp = Head->next;
	if (Head->next == NULL) // 判断当前链表是否为空,为空则直接退出
	{
		printf("current linkeflist is empty!\n");
		return false;
	}
	while (tmp->data != destval && tmp->next != NULL) // find the specific node找到指定值的节点
	{
		tmp = tmp->next;
	}
	if (tmp->data == destval && tmp == Head->next)
	{
		Head->next = tmp->next; // 如果存在目标值,且目标值位于首节点时,进行头删操作
		Head->next->prev = NULL;
		tmp->next = NULL;
		tmp->prev = NULL;
		free(tmp);
		return true;
	}
	else if (tmp->data == destval) // 如果存在目标值,且目标值不位于首节点时,进行删除操作
	{
		tmp->prev->next = tmp->next;
		tmp->next->prev = tmp->prev;
		tmp->prev = NULL;
		tmp->next = NULL;
		free(tmp);
		return true;
	}
	else // 如果不存在目标值,则直接返回
	{
		printf("The is no destival\n");
		return false;
	}
}

功能函数:遍历打印链表

bool DoubleLList_Print(DoubleLList_t *Head)
{
	// 对链表头结点的地址进行备份
	DoubleLList_t *Phead = Head;
	// 判断当前链表是否为空,为空则直接退出
	if (Head->next == Head)
	{
		printf("current linkeflist is empty!\n");
		return false;
	}
	// 从首结点开始遍历
	while (Phead->next)
	{
		// 把头结点的直接后继作为新的头结点
		Phead = Phead->next;
		// 输出头结点的直接后继的数据域
		printf("data = %d  ", Phead->data);
		// 判断是否到达尾结点,尾结点的next指针是指向首结点的地址
		if (Phead->next == Head->next)
		{
			break;
		}
	}
	return true;
}

主函数,调用并测试各功能函数

int main(int argc, char const *argv[])
{
	DoubleLList_t *H = DoubleLList_Create();
	DoubleLList_HeadInsert(H, 20);
	DoubleLList_HeadInsert(H, 30);
	//DoubleLList_HeadInsert(H, 10);
	DoubleLList_TailInsert(H, 40);
	DoubleLList_TailInsert(H, 20);
	DoubleLList_TailInsert(H, 50);
	DoubleLList_DestInsert(H, 30, 31);
	DoubleLList_DestInsert(H, 32, 31);

	DoubleLList_Print(H);
	puts("");
	DoubleLList_HeadDel(H);
	printf("1\n");
	DoubleLList_TailDel(H);
	printf("2\n");
	DoubleLList_MidDel(H, 31);
	printf("3\n");

	DoubleLList_Print(H);
	puts("");
	return 0;
}

热门相关:剑道邪尊Ⅱ   隐婚99天:首长,请矜持   重生之大文豪   史上第一密探   我在镇夜司打开地狱之门