html

数据结构:双向链表的创建·插入·删除

数据结构:双向链表的创建·插入·删除

/**
  * @file name : 数据结构:双向链表的创建·插入·删除
  * @brief     : 实现双向链表的创建·插入·删除
  * @author	   : liuliu@163.com
  * @date 	   :2024/04/23
  * @version   :1.0
  * @note      :none
  * CopyRight(c):  2023-2024   liuliu@163.com   All Right Reseverd
    */

  //定义节点的数据域类型
  typedef int data_type;
  //定义节点的类型并起别名
  typedef struct Double_LList{
  	data_type data;
  	struct Double_LList *next;//后驱的指针域
  	struct Double_LList *perv;//前驱的指针域
  }DouLList_t;
//创建一个空链表,其中只包含一个头节点
DouLList_t * DouLList_Create()
  {
  	DouLList_t *Head=(DouLList_t *)calloc(1,sizeof(	DouLList_t ));
  	//判断是否申请成功
	if(NULL==Head)
	{
		perror("calloc memory for Head is failed");
		exit(-1);
	}
	//初始化前驱和后驱指针并返回头节点的地址
	Head->next=NULL;
	Head->prev=NULL;
	return Head;
  }
//创建一个新节点
DouLList_t * DouLList_Newnode_Create(data_type data)
{
	DouLList_t * New=(DouLList_t *)calloc(1,sizeof(DouLList_t ));//为新节点申请一块内存
	//判断内存是否申请成功
	if(NULL==New)
	{
		perror("calloc memory for New is failed");
		return NULL;
	}
	//初始化前驱和后驱指针并返回头节点的地址
	New->next=NULL;
	New->prev=NULL;
	New->data=data;
	return New;
}
//判断是否为空链表
bool DouLList_Isful(DouLList_t *Head)
{
	if(Head->next==NULL)
	{
		return ture;
	}
	return false;
}
//从头部插入一个节点
bool DouLList_HeadAdd(DouLList_t *Head,DouLList_t *New)
{

	if(New==NULL)
	{
		return false;
	}
	//如果链表为空
	if(DouLList_Isful(DouLList_t *Head))
	{
		Head->next=New;
		return ture;
	}

	//链表不为空
	New->next=Head->next;
	Head->next->prev=New;
	Head->next=New;
	return ture;
}

//从尾部插入一个节点
bool DouLList_TailAdd(DouLList_t *Head,DouLList_t *New)
{
	DouLList_t * current=Head->next;
	if(New==NULL)
	{
		return false;
	}
	//如果链表为空
	if(DouLList_Isful(DouLList_t *Head))
	{
		Head->next=New;
		return ture;
	}
	//链表不为空,先遍历到最后一个节点
	while(current)
	{
		if(NULL==current->next)
		{
			brake;
		}
		current=current->next;
	}
	current->next=New;
	New->prev=current;
	return ture;
}
//从中间插入一个节点
bool DouLList_MidAdd(DouLList_t *Head,DouLList_t *New,DouLList_t *Dest)
{
	DouLList_t * current=Head->next;
	if(New==NULL)
	{
		return false;
	}
	//如果链表为空
	if(DouLList_Isful(DouLList_t *Head))
	{
		Head->next=New;
		return ture;
	}
	//链表不为空,遍历到最后目标节点
	while(current)
	{
		if(Dest—>data==current->data)
		{
			brake;//找到了就跳出循环
		}
		current=current->next;
	}
	New->next=current->next;
	current->prev=New;
	New->prev=current;
	current->next=New;
	return ture;
}
//从头部删除节点
bool DouLList_HeadDel(DouLList_t *Head)
{
	DouLList_t *phead=Head->next;//记录首节点地址
	//如果链表为空
	if(DouLList_Isful(DouLList_t *Head))
	{
		return false;
	}
	//链表不为空
	Head->next=Head->next->next;
	phead->next=NULL;
	phead->next->prev=NULL;
	free(phead);//释放内存
	return ture;	
}
//从尾部删除节点
bool DouLList_TailDel(DouLList_t *Head)
{
	DouLList_t *current=Head->next;//记录当前节点地址
	//如果链表为空
	if(DouLList_Isful(DouLList_t *Head))
	{
		return false;
	}
	//链表不为空,遍历找到尾部节点
	while(current)
	{
		if(current->next==NULL)
		{
			brake;//找到了就跳出循环
		}
		current=current->next;
	}
	current->prev->next=NULL;
	current->prev=NULL;
	free(current);//释放被删除的节点内存
	return ture;
}
//从中间删除节点
bool DouLList_MidDel(DouLList_t *Head,DouLList_t *Dest)//接收要删除的目标节点
{
	DouLList_t *current=Head->next;//记录当前节点地址
	//如果链表为空
	if(DouLList_Isful(DouLList_t *Head))
	{
		return false;
	}
	//链表不为空,遍历找到目标节点
	while(current)
	{
		if(current->data==Dest->data)
		{
			brake;找到了就跳出循环
		}
		current=current->next;
	}
	current->prev->next=current->next;
	current->next->prev=current->prev;
	current->prev=NULL;
	current->next=NULL;
	free(current);//释放被删除的节点内存
	return ture;
}



posted @ 2024-04-24 09:05  大螺丝贼六  阅读(48)  评论(0)    收藏  举报
html