双向链表增删改查

数据结构

双向链表

双向链表增删改查



/*************************************************************************************
 *
 *  file name:  1.c
 *  author   : 	lu.ciana.598393@gmail.com
 *  date     :  2024/04/23
 *  function : 	双向链表的增删改查
 *  note     :  None
 *  CopyRight (c)   2024    lu.ciana.598393@gmail.com   All Right Reserved
 *
 ************************************************************************************/

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


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

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

}DoubleLList_t;

/**
  * @name:		DoubleLList_Create
  * @brief 		创建一个空双向链表,空链表应该有一个头结点,对链表进行初始化
  * @retval 	DoubleLList_t *
  * @date 		2024/04/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:		DoubleLList_NewNode
  * @brief  	创建新的结点,并对新结点进行初始化(数据域 + 指针域)
  * @param  
  				@data
  * @retval 	DoubleLList_t * 
  * @date 		2024/04/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
  * @brief  	头插
  * @param  
  				@data
  				@Head
  * @retval 	bool
  * @date 		2024/04/23
  * @note   	补充 注意 说明
  */

bool DoubleLList_HeadInsert(DoubleLList_t *Head,DataType_t data)
{
	DoubleLList_t *PHead =Head;
	DoubleLList_t *New = DoubleLList_NewNode(data);	
	//如果申请内存失败
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return false;
	}
	//如果是空链表,直接插入
	if (NULL == Head->next)
	{
		Head->next = New;
		New->next = NULL;
		return true;
	}

	New->next = Head->next;
	Head->next->prev = New;
	Head->next=New;
	return true;
}

/**
  * @name:		DoubleLList_TailInsert
  * @brief  	尾插
  * @param  
  				@data
  				@Head
  * @retval 	bool
  * @date 		2024/04/23
  * @note   	补充 注意 说明
  */
bool DoubleLList_TailInsert(DoubleLList_t *Head,DataType_t data)
{
	DoubleLList_t *PHead =Head;
	DoubleLList_t *New = DoubleLList_NewNode(data);	
	//如果申请内存失败
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return false;
	}
	//如果是空链表,直接插入
	if (Head->next == NULL)
	{
		Head->next = New;
		New->next = NULL;
		return true;
	}
	//遍历获取最后的值
	while(PHead->next)
		PHead = PHead ->next;

	PHead->next=New;
	New->prev= PHead;
	return true;
}

/**
  * @name:		DoubleLList_DestInsert
  * @brief  	指定位置插入
  * @param  
  				@data
  				@Head
  				@destval
  * @retval 	bool
  * @date 		2024/04/23
  * @note   	补充 注意 说明
  */

bool DoubleLList_DestInsert(DoubleLList_t *Head,DataType_t destval,DataType_t data)
{
	DoubleLList_t *PHead =Head;
	DoubleLList_t *New = DoubleLList_NewNode(data);	
	//如果申请内存失败
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return false;
	}
	//如果是空链表,直接插入
	if (Head->next == Head)
	{
		Head->next = New;
		New->next = NULL;
		return true;
	}
	//遍历获取目标值
	while(PHead->next)
	{
		PHead = PHead ->next;
		if(destval == PHead->data)
			break;
	}
	New->next = PHead->next;
	PHead->prev=New;
	New->prev=PHead;
	PHead->next=New;
	return true;
}
/**
  * @name:		DoubleLList_HeadDel
  * @brief  	头删
  * @param  
  				@Head
  * @retval 	bool
  * @date 		2024/04/23
  * @note   	补充 注意 说明
  */
bool DoubleLList_HeadDel(DoubleLList_t *Head)
{
	//对双向循环链表的头结点的地址进行备份
	DoubleLList_t *PHead = Head->next;
	//链表为空
	if (Head == Head->next)
        return false;
		//链表内只有一个节点
//	if(Head->next == NULL)
//	{
//		PHead->next = NULL;
//		PHead->prev->next = NULL;
//		free(PHead);
//		return true;
//	}
	Head->next = PHead->next;
	PHead->next = NULL;
	Head->next->prev = NULL;
	free(PHead);
	return true;
}
/**
  * @name:		DoubleLList_TailDel
  * @brief  	尾删
  * @param  
  				@Head
  * @retval 	bool
  * @date 		2024/04/23
  * @note   	补充 注意 说明
  */

bool DoubleLList_TailDel(DoubleLList_t *Head)
{
	//对双向循环链表的头结点的地址进行备份
	DoubleLList_t *PHead = Head->next;
	//链表为空
	if (Head == Head->next)
        return false;
	//遍历获得尾结点
	while(PHead->next)
	{
		PHead = PHead->next;
	}
		PHead->prev->next=NULL;
		PHead->prev=NULL;
		free(PHead);
}
/**
  * @name:		DoubleLList_DestDel
  * @brief  	指定位置删
  * @param  
  				@Head
  				@destval
  * @retval 	bool
  * @date 		2024/04/23
  * @note   	补充 注意 说明
  */
bool DoubleLList_DestDel(DoubleLList_t *Head,DataType_t destval)
{
	//对双向循环链表的头结点的地址进行备份
	DoubleLList_t *PHead = Head->next;
	DoubleLList_t *LHead = Head;
	//链表为空
	if (Head == Head->next)
        return false;
	
	//遍历获得尾结点及其直接前驱
	while(LHead->next)
	{
		PHead = PHead->next;
		if(destval == PHead->data)
			break;
	}
	PHead->prev->next = PHead->next;
	PHead->next->prev=PHead->prev;
	PHead->prev=NULL;
	PHead->next=NULL;
	free(PHead);
}
/**
  * @name:		DoubleLList_Print
  * @brief  	遍历链表
  * @param  
  				@Head
  * @retval 	bool
  * @date 		2024/04/23
  * @note   	补充 注意 说明
  */
bool DoubleLList_Print(DoubleLList_t *Head)
{
	//对双向循环链表的头结点的地址进行备份
	DoubleLList_t *Phead = Head;
	
	//判断当前链表是否为空,为空则直接退出
	if (Head->next == BULL)
	{
		printf("current doublelinkeflist is empty!\n");
		return false;
	}

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

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

		//判断是否到达尾结点,尾结点的next指针是指向首结点的地址
		if (Phead->next == NULL)
		{
			break;
		}	
	}
	return true;
}

//调用函数测试
int main(int argc, char const *argv[])
{
	DoubleLList_t  * Head=DoubleLList_Create();
	DoubleLList_TailInsert(Head,2);
	DoubleLList_TailInsert(Head,3);
	DoubleLList_TailInsert(Head,4);
	DoubleLList_TailInsert(Head,5);
	DoubleLList_TailInsert(Head,6);


	DoubleLList_HeadDel(Head);
	// DoubleLList_HeadDel(Head);
	DoubleLList_TailDel(Head);
	DoubleLList_DestDel(Head,4);
	// DoubleLList_DestDel(Head,5);

	DoubleLList_Print(Head);
	return 0;
}


posted @ 2024-04-24 10:11  luxiaolim  阅读(14)  评论(0)    收藏  举报