链接表的增删

链接表的增删

/***********************************************************************************
 * file name  :demo.c
 * cauthor    :qp686868mx@126.com
 * date       :2024/04/22
 * function   :创建链接表并在顺序表中进行增删
 * note       :none
 * CopyRight (c)   2023-2024     qp686868@126.com     All Right Reseverd
 ************************************************************************************/


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

//构造链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct LinkedList
{
	DataType_t  		 data; //结点的数据域
	struct LinkedList	*next; //结点的指针域

}LList_t;

/*******************************************************************
*
*	函数名称:	LList_Create
*	函数功能:  创建一个空链表,空链表应该有一个头结点,对链表进行初始化
* 	函数参数:  void
*   返回结果:   局部变量的地址
* 	注意事项:   None
* 	函数作者:   qp686868@126.com
*	创建日期:   2024/04/22
*	修改历史:
*	函数版本:	V1.0
* *****************************************************************/
//创建一个空链表,空链表应该有一个头结点,对链表进行初始化
LList_t * LList_Create(void)
{
	//1.创建一个头结点并对头结点申请内存
	LList_t *Head = (LList_t *)calloc(1,sizeof(LList_t));
	if (NULL == Head)
	{
		perror("Calloc memory for Head is Failed");
		exit(-1);
	}

	//2.对头结点进行初始化,头结点是不存储有效内容的!!!
	Head->next = NULL;

	//3.把头结点的地址返回即可
	return Head;
}
/*******************************************************************
*
*	函数名称:	LList_NewNode
*	函数功能:  创建新的结点,并对新结点进行初始化(数据域 + 指针域)
* 	函数参数:  data
*   返回结果:   局部变量的地址
* 	注意事项:   None
* 	函数作者:   qp686868@126.com
*	创建日期:   2024/04/22
*	修改历史:
*	函数版本:	V1.0
* *****************************************************************/
//创建新的结点,并对新结点进行初始化(数据域 + 指针域)
LList_t * LList_NewNode(DataType_t data)
{
	//1.创建一个新结点并对新结点申请内存
	LList_t *New = (LList_t *)calloc(1,sizeof(LList_t));
	if (NULL == New)
	{
		perror("Calloc memory for NewNode is Failed");
		return NULL;
	}

	//2.对新结点的数据域和指针域进行初始化
	New->data = data;
	New->next = NULL;

	return New;
}
/*******************************************************************
*
*	函数名称:	LList_HeadInsert
*	函数功能:  在头部插入一个结点
* 	函数参数:  Head   data
*   返回结果:   插入成功或失败
* 	注意事项:   None
* 	函数作者:   qp686868@126.com
*	创建日期:   2024/04/22
*	修改历史:
*	函数版本:	V1.0
* *****************************************************************/
//在头部插入一个结点
bool LList_HeadInsert(LList_t *Head,DataType_t data)
{
	//1.创建新的结点,并对新结点进行初始化
	LList_t *New = LList_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->next = New;

	return true;
}



/*******************************************************************
*
*	函数名称:	LList_TailInsert
*	函数功能:  在尾部插入一个结点
* 	函数参数:  *Head   data
*   返回结果:   插入成功或失败
* 	注意事项:   None
* 	函数作者:   qp686868@126.com
*	创建日期:   2024/04/22
*	修改历史:
*	函数版本:	V1.0
* *****************************************************************/
//在尾部插入一个结点
bool LList_TailInsert(LList_t *Head,DataType_t data)
{
	//1.创建新的结点,并对新结点进行初始化
	LList_t *New = LList_NewNode(data);
	if (NULL == New)
	{
		printf("can not insert new node\n");
		return false;
	}
	//对链表的头文件的地址进行备份
	LList_t *Phead = Head;
	
	//首结点
	while(Phead->next){
		//把头的直接后继作为新的头结点
		Phead = Phead->next;
	}
	Phead->next=New;
	New->next=NULL;
	return true;
}
/*******************************************************************
*
*	函数名称:	LList_TailInsert
*	函数功能:  在链接表中部插入一个结点
* 	函数参数:  *Head    dest   data
*   返回结果:   插入成功或失败
* 	注意事项:   None
* 	函数作者:   qp686868@126.com
*	创建日期:   2024/04/22
*	修改历史:
*	函数版本:	V1.0
* *****************************************************************/
//在链接表中部插入一个结点
bool LList_MediInsert(LList_t *Head,DataType_t dest,DataType_t data)
{
	//1.创建新的结点,并对新结点进行初始化
	LList_t *New = LList_NewNode(data);
	if (NULL == New)
	{
		printf("can not insert new node\n");
		return false;
	}
	//对链表的头文件的地址进行备份
	LList_t *Phead1 = Head;
	LList_t *Phead2=Head;
	for(int i=0;i<dest;i++){
		Phead1=Phead1->next;
        
	}
	for(int j=0;j<=dest;i++){
		Phead=Phead2->next
	}
	New=Phead2->next;
	Phead1=New->next;
	return true;
	
}
/*******************************************************************
*
*	函数名称:	LList_TailInsert
*	函数功能:  在链接表头部删除一个结点
* 	函数参数:  *Head   data
*   返回结果:   删除成功或失败
* 	注意事项:   None
* 	函数作者:   qp686868@126.com
*	创建日期:   2024/04/22
*	修改历史:
*	函数版本:	V1.0
* *****************************************************************/
bool LList_HeadDel(LList_t *Head,DataType_t data)
{
	if(NULL==Head->next){
		return false;
	}
	Head=Head->next->next;
	Head->next=NULL;
	free(Head->next);
	return true;
}
/*******************************************************************
*
*	函数名称:	LList_TailInsert
*	函数功能:  在链接表尾部删除一个结点
* 	函数参数:  *Head   data
*   返回结果:   删除成功或失败
* 	注意事项:   None
* 	函数作者:   qp686868@126.com
*	创建日期:   2024/04/22
*	修改历史:
*	函数版本:	V1.0
* *****************************************************************/
LList_TailDel(LList_t *Head,DataType_t data)
{
	if(NULL==Head->next){
		return false;
	}
		//对链表的头文件的地址进行备份
	LList_t *Phead = Head;
	while(Phead->next){
		//把头的直接后继作为新的头结点
		Phead = Phead->next;
	}
	Phead=NULL;
	free(Phead->next);
	return true;
}
/*******************************************************************
*
*	函数名称:	LList_MediDel
*	函数功能:  在链接表中部删除一个结点
* 	函数参数:  *Head   data
*   返回结果:   删除成功或失败
* 	注意事项:   None
* 	函数作者:   qp686868@126.com
*	创建日期:   2024/04/22
*	修改历史:
*	函数版本:	V1.0
* *****************************************************************/
bool LList_MediDel(LList_t *Head,DataType_t dest,DataType_t data)
{
if(NULL==Head->next){
		return false;
	}
	LList_t *Phead1=Head;
	//LList_t *Phead2=Head;
	//int numbr=o;
	while(Phead1->next){
		Phead1=Phead1->next;
		//number++;
		if(Phead1->data==data)
		break;
	}


}
//遍历
void LList_Print(LList_t *Head)
{
	//对链表的头文件的地址进行备份
	LList_t *Phead = Head;
	
	//首结点
	while(Phead->next)
	{
		//把头的直接后继作为新的头结点
		Phead = Phead->next;

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

}


  bool LList_DeleMin(LList_t* Head)
  {
	if (NULL==Head) 
    return false;
  //对链表的头文件的地址进行备份
  LList_t* Phead1 = Head;
  LList_t* Phead2 = Head;
  LList_t* Phead3 = Head;
  LList_t* Phead4 = Head;
  int min = Head->data;
   while(Phead1->next){
	Phead1=Phead1->next;
	if(Phead1->data>Phead1->next->data){
	min=Phead1->next->data;
	}
   }
   //判断最小值的结点在头部时
   if(min==Head->data){
	Head=Head->next->next;
	Head->next=NULL;
	free(Head->next);
	return true;
   }
   //判断最小值的结点在尾部时
   if(NULL==Phead1->next){
	while(Phead2->next){
		//把头的直接后继作为新的头结点
		Phead2 = Phead2->next;
	}
	Phead2=NULL;
	free(Phead2->next);
	return true;
   }
  }
int main(int argc, char const *argv[])
{
	
	return 0;
}
  /*******************************************************************
*
*	函数名称:	LList_Find
*	函数功能:  在链接表查找倒数第k个位置上的值
* 	函数参数:  *Head   k
*   返回结果:   查找成功或失败
* 	注意事项:   None
* 	函数作者:   qp686868@126.com
*	创建日期:   2024/04/22
*	修改历史:
*	函数版本:	V1.0
* *****************************************************************/
int LList_Find(LList_t *Head, int k)
{  
LList_t *Phead1 = Head->next;
LList_t *Phead2 = Head->next;
int number = 0; 

while (Phead1->next)
{
    Phead1 = Phead1->next; 
    number++;
}
if (k >number)
    return 0;
for (int i = 0; i < mumber - k; i++)
{
    Phead2 = Phead2->next;
}
printf("K-th to last data = %d\n", Phead2->data);
return 1;
}
int main(int argc, char const *argv[])
{
	
	return 0;
}
posted @ 2024-04-22 23:46  红枫叶$$  阅读(10)  评论(0)    收藏  举报