首页 > 其他分享 >双向链表接口设计

双向链表接口设计

时间:2024-04-24 10:01:00浏览次数:15  
标签:tmp DoubleLList 结点 Head 接口 next 链表 双向 NULL

双向链表接口设计

/**

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

标签:tmp,DoubleLList,结点,Head,接口,next,链表,双向,NULL
From: https://www.cnblogs.com/cino/p/18154430

相关文章

  • JS之调用高德地图接口进行打卡
    调用高德地图接口进行打卡1.安装依赖"@amap/amap-jsapi-loader":"^1.0.1"2.增加代码如下:orientation.jsimportAMapLoaderfrom'@amap/amap-jsapi-loader';import{gcj02towgs84}from'./coordTransform.js';exportfunctiongetOrientati......
  • C语言单向循环链表的增删操作
    /***********************************************************************************************************设计单向循环链表的接口****Copyright(c)[email protected]**********************************************......
  • 删除单链表中最小值的结点
    /***********************************************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......