首页 > 其他分享 >链接表的增删

链接表的增删

时间:2024-04-22 23:55:55浏览次数:15  
标签:Head return 结点 next LList 链接表 增删 data

链接表的增删

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


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

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

}LList_t;

/*******************************************************************
*
*	函数名称:	LList_Create
*	函数功能:  创建一个空链表,空链表应该有一个头结点,对链表进行初始化
* 	函数参数:  void
*   返回结果:   局部变量的地址
* 	注意事项:   None
* 	函数作者:   [email protected]
*	创建日期:   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
* 	函数作者:   [email protected]
*	创建日期:   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
* 	函数作者:   [email protected]
*	创建日期:   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
* 	函数作者:   [email protected]
*	创建日期:   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
* 	函数作者:   [email protected]
*	创建日期:   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
* 	函数作者:   [email protected]
*	创建日期:   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
* 	函数作者:   [email protected]
*	创建日期:   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
* 	函数作者:   [email protected]
*	创建日期:   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
* 	函数作者:   [email protected]
*	创建日期:   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;
}

标签:Head,return,结点,next,LList,链接表,增删,data
From: https://www.cnblogs.com/cr886/p/18151859

相关文章

  • C语言单向链表的创建和增删减查相关程序
    对单向链表进行删除和插入的程序设计/********************************************************************* filename: linkedlist.c* author :[email protected]* date :2024/04/22* function:实现单向链表的创建、拆入、删除功能* note :None**......
  • C语言实现链表增删减改
    /********************************************************************filename: main.cauthor :[email protected] :2024/04/22function:实现对链表的增删改查note :NoneCopyRight(c)[email protected]***......
  • express + mysql + jwt 简单的增删改查
    gitee地址https://gitee.com/newly-released_0/express-mysql-jwtjwt的代码constexpress=require('express')constapp=express()constcompression=require('compression');constcors=require('cors')//导入jsonwebtoken和express......
  • 【开源】使用Python+Flask+Mysql快速开发一个用户增删改查系统
    项目演示项目本身很简单,增删改查是几乎所有系统的骨架。正所谓万丈高楼平地起,学会了增删改查,航母就指日可待了:),光速入门,直接看演示图:项目地址https://github.com/mudfish/python-flask-user-crudFlask框架介绍说白了就是一个Web框架,能够让你快速开发出Pythonweb应用。简单易......
  • HarmonyOS NEXT应用开发之Tab组件实现增删Tab标签
    介绍本示例介绍使用了Tab组件实现自定义增删Tab页签的功能。该场景多用于浏览器等场景。效果图预览使用说明:点击新增按钮,新增Tab页面。点击删除按钮,删除Tab页面。实现思路设置Tab组件的barHeight为0,隐藏组件自带的TabBar。Tabs(){...}.barHeight(0)//隐藏tab......
  • go语言使用go-elasticsearch/v8如何操作es8.x版本实现索引的增删查改
    import("context""encoding/json""fmt""github.com/elastic/go-elasticsearch/v8""github.com/elastic/go-elasticsearch/v8/esapi""github.com/elastic/go-elasticsearch/v8/esutil&......
  • SpringBoot+MybatisPlus 增删改查学习第三章 (C#转JAVA)
    packagecom.example.demo;importcom.baomidou.mybatisplus.core.conditions.query.QueryWrapper;importcom.example.demo.entity.Person;importcom.example.demo.mapper.PersonMapper;importcom.example.demo.service.PersonService;importorg.junit.jupiter.api.Test;i......
  • Python对Sql Server数据库增删改查
    Python对SqlServer数据库增删改查#如果电脑上没有安装mssql模块,则要安装mssql模块 安装模块的执行命令为 pip install pymssqlimport pymssql def ExecuteNonQuery(sqlStr,paras):    try:        connect = pymssql.connect(server='192.168.1.23:1......
  • 增删改查
    --1切换到master数据库中usemastergo--2判断cpms数据库是否存在,若存在则删除,begin和end可以相当于c#中的大括号{}ifexists(select*fromsys.sysdatabaseswherename='cpms')--存在ture不存在falsedropdatabasecpmsgo--3创建cpms数据库createdatabasecpmso......
  • # C++之STL整理(7)之queue用法(创建、赋值、增删查改)详解
    C++之STL整理(7)之queue用法(创建、赋值、增删查改)详解注:整理一些突然学到的C++知识,随时mark一下例如:忘记的关键字用法,新关键字,新数据结构C++的queue用法整理C++之STL整理(7)之queue用法(创建、赋值、增删查改)详解queue1.queue构造函数2.queue存取、插入和删除操作3.......