单向顺序链表程序接口

单向顺序链表程序接口

头函数、程序说明

image

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*******************************************************************
 *
 *	file name:	单向顺序链表程序接口
 *	author	 :  17647576169@163.com
 *	date	 :	2024-4-22
 *	function :	单向顺序链表的创建,增,删,减,查
 * 	note	 :  None
 *
 *	CopyRight (c)  2024   17647576169@163.com   All Right Reseverd
 *
 * *****************************************************************/

链表、节点的创建

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

// 构造链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct LinkedList
{
	DataType_t data;		 // 结点的数据域
	struct LinkedList *next; // 结点的指针域
} LList_t;
/********************************************************************
 *
 *	name	 :	LList_Create
 *	function :  创建一个空的单向顺序链表,空链表应该有一个头结点,对链表进行初始化
 *	argument :	None
 *
 *	retval	 :  返回创建的链表的头地址
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :	None
 *
 * *****************************************************************/
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;
}
/********************************************************************
 *
 *	name	 :	LList_NewNode
 *	function :  创建新的结点,并对新结点进行初始化(数据域 + 指针域)
 *	argument :	@data:需插入的数据
 *	retval	 :  返回新创建链表节点的地址
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/
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;
}

三种数据插入方式

image

在链表头部进行插入

/********************************************************************
 *
 *	name	 :	LList_HeadInsert
 *	function :  向链表的头部进行数据插入
 *	argument :	@head:目标链表
 *				@data:需插入的数据
 *	retval	 :  返回新创建链表节点的地址
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/
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;
}

向链表的尾部进行数据插入

/********************************************************************
 *
 *	name	 :	LList_TailInsert
 *	function :  向链表的尾部进行数据插入
 *	argument :	@head:目标链表
 *				@data:需插入的数据
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/
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;
	}
	// 2.判断链表是否为空,如果为空,则直接插入即可
	if (NULL == Head->next)
	{
		Head->next = New;
		return true;
	}
	// 3对链表的头文件的地址进行备份
	LList_t *Phead = Head;
	// 4遍历链表找到尾部
	while (Phead->next)
	{
		// 把头的直接后继作为新的头结点
		Phead = Phead->next;
	}
	// 5.把新结点插入到链表的尾部
	Phead->next = New;
	return true;
}

向链表的指定数据节点后插入

/********************************************************************
 *
 *	name	 :	LList_TailInsert
 *	function :  向链表的指定数据节点后插入
 *	argument :	@head:目标链表
 *				@data:需插入的数据
 *				@dest:插入位置
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/
bool LList_Insert(LList_t *Head, DataType_t destval, DataType_t data)
{
	// 备份头节点
	LList_t *Phead = Head;
	// 备份首节点
	LList_t *Temp = Head->next;
	// 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;
	}
	// 遍历链表当链表到尾部或找的目标数据节点是停止
	while (Temp != NULL && destval != Temp->data)
	{
		Temp = Temp->next;
	}
	// 未找到目标数据节点
	if (destval != Temp->data)
	{
		return false;
	}
	// 找的节点开始插入
	New->next = Temp->next; // 新节点的后继指向目标节点的后继
	Temp->next = New;		// 目标节点指向新节点
	return true;
}

三种数据删除方式

image

删除链表头部节点

/********************************************************************
 *
 *	name	 :	LList_HeadDel
 *	function :  删除链表头部节点
 *	argument :	@head:目标链表
 *
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/
bool LList_HeadDel(LList_t *Head)
{
	// 备份头节点
	LList_t *Phead = Head;
	// 备份首节点
	LList_t *Temp = Head->next;
	if (NULL == Head->next)
	{
		return false;
	}
	// 非空
	Head->next = Temp->next; // 头节点指向首节点的后继
	Temp->next = NULL;		 // 首节点指向空
	free(Temp);				 // 释放首节点
	return true;
}

删除链表尾部节点

/********************************************************************
 *
 *	name	 :	LList_TailDel
 *	function :  删除链表尾部节点
 *	argument :	@head:目标链表
 *				@data:需删除的数据
 *
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/
bool LList_TailDel(LList_t *Head)
{
	// 备份头节点
	LList_t *Phead = Head;
	// 备份头节点
	LList_t *Temp = Head->next;
	if (NULL == Head->next)
	{
		return false;
	}
	// 节点只有一个,也可调用头删除
	if (NULL == Temp->next)
	{
		Temp->next = NULL;
		Head->next = NULL;
		free(Temp);
		return true;
	}
	// 非空,遍历找到尾结点
	while (Temp->next)
	{
		Temp = Temp->next;
		Phead = Phead->next;
	}
	Phead->next = NULL; // 尾结点前驱释放
	Temp->next = NULL;	// 尾结点指针释放
	free(Temp);			// 释放尾结点堆空间
	return true;
}

删除链表中指定数据的节点

bool LList_DestInsert(LList_t *Head, DataType_t data)
{
	// 备份头节点
	LList_t *Phead = Head;
	// 备份头节点
	LList_t *Temp = Head->next;
	// 判断链表是否为空
	if (NULL == Head->next)
	{
		return false;
	}
	// 要删除的节点在第一个
	if (data == Head->next->data)
	{
		LList_HeadDel(Head);
		return true;
	}
	// 非空,遍历链表记录目标节点及其前驱
	while (Temp->next)
	{
		Temp = Temp->next;
		Phead = Phead->next;
		if (data == Temp->data)
		{
			break;
		}
	}
	// 未找到目标节点
	if (data != Temp->data)
	{
		perror("The target data cannot be found\n");
		return false;
	}
	// 找的节点开始删除
	else
	{
		Phead->next = Temp->next; // 前驱指向后继
		Temp->next = NULL;		  // 释放指针
		free(Temp);				  // 释放堆空间
		return true;
	}
}

遍历链表

/********************************************************************
 *
 *	name	 :	LList_Print
 *	function :  遍历链表
 *	argument :	@head:目标链表
 *
 *
 *	retval	 :  none
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/
void LList_Print(LList_t *Head)
{
	// 对链表的头文件的地址进行备份
	LList_t *Phead = Head;
	// 首结点
	while (Phead->next)
	{
		// 把头的直接后继作为新的头结点
		Phead = Phead->next;

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

题:删除单向链表中最小值节点

/********************************************************************
 *
 *	name	 :	LList_Print
 *	function :  删除单向链表中最小值节点
 *	argument :	@Head:目标链表
 *	retval	 :  none
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/
void LList_DelMin(LList_t *Head)
{
	LList_t *min_prev = Head;
	LList_t *min = Head->next;
	LList_t *Phead = Head->next;
	LList_t *Phead_prev = Head;
	// 遍历链表赵最小
	while (Phead->next)
	{
		if (min->data > Phead->next->data)
		{
			min = Phead->next;
			min_prev = Phead;
		}
		Phead_prev = Phead;
		Phead = Phead->next;
	}
	min_prev->next = min->next;
	min->next = NULL;
	free(min);
}

程序验证

int main(int argc, char const *argv[])
{
	LList_t *Manager = LList_Create();
	// 头插入
	LList_HeadInsert(Manager, 7);
	LList_HeadInsert(Manager, 6); // 6 7
	LList_Print(Manager);
	printf("\n");
	// 尾插人
	LList_TailInsert(Manager, 2);
	LList_TailInsert(Manager, 1);
	LList_TailInsert(Manager, 5); // 6 7 2 1 5
	LList_Print(Manager);
	printf("\n");
	// 1后面插入0
	LList_Insert(Manager, 1, 0); // 6 7 2 1 0 5
	LList_Print(Manager);
	printf("\n");
	// 删除最小值节点
	LList_DelMin(Manager); // 6 7 2 1 5
	LList_Print(Manager);
	printf("\n");
	// 首删除
	LList_HeadDel(Manager); // 7 2 1 5
	LList_Print(Manager);
	printf("\n");
	// 尾删除
	LList_TailDel(Manager); // 7 2 1
	LList_Print(Manager);
	printf("\n");
	// 删除2
	LList_DestInsert(Manager, 2); // 7 1
	LList_Print(Manager);
	printf("\n");
	// 删除9
	LList_DestInsert(Manager, 9); // 7 1
	LList_Print(Manager);
	printf("\n");
	return 0;
}

输出结果

data = 6
data = 7

data = 6
data = 7
data = 2
data = 1
data = 5

data = 6
data = 7
data = 2
data = 1
data = 0
data = 5

data = 6
data = 7
data = 2
data = 1
data = 5

data = 7
data = 2
data = 1
data = 5

data = 7
data = 2
data = 1

data = 7
data = 1

The target data cannot be found
: Success
data = 7
data = 1
posted @ 2024-04-22 20:25  谁TM买小米啊  阅读(41)  评论(1编辑  收藏  举报