单向顺序链表的创建,增,删,减,查
  6Df3JnWQUC5m 12天前 17 0

单向顺序链表的创建,增,删,减,查

/*******************************************************************
 *
 *	file name:	单向顺序链表的创建,增,删,减,查
 *	author	 :  17647576169@163.com
 *	date	 :	2024-4-22
 *	function :
 * 	note	 :  None
 *
 *	CopyRight (c)  2024   17647576169@163.com   All Right Reseverd
 *
 * *****************************************************************/

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// 指的是单向链表中的结点有效数据类型,用户可以根据需要进行修改
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	 :
 *
 * *****************************************************************/

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;
}

/********************************************************************
 *
 *	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_TailInsert(LList_t *Head, DataType_t data, int dest)
{
	// 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;
	}
	if (NULL == Head->next->next)
	{
		Head->next->next = New;
		return true;
	}
	// 定义两指针备份首节点地址及其后置
	LList_t *P1 = Head->next;
	LList_t *P2 = P1->next;
	// 偏移找的插入位置
	for (int i = 0; i < dest - 1; i++)
	{
		P1 = P1->next;
		P2 = P2->next;
	}
	// 把新结点插入
	New->next = P2->next;
	P1->next = New;
	P2->next = NULL;
	free(P2);
	return true;
}

/********************************************************************
 *
 *	name	 :	LList_DestInsert
 *	function :  删除链表中的数据
 *	argument :	@head:目标链表
 *				@data:需删除的数据
 *
 *	retval	 :  返回1成功0失败
 *	author	 :  17647576169@163.com
 *	date	 :  2024-4-22
 * 	note	 :
 *
 * *****************************************************************/

bool LList_DestInsert(LList_t *Head, DataType_t data)
{
	// 判断链表是否为空
	if (NULL == Head->next)
	{
		return false;
	}
	// 要删除的节点在第一个
	if (data == Head->next)
	{
		// 备份首节点地址
		LList_t *P = Head;
		Head->next = Head->next->next;
		P->next = NULL;
		free(P);
		return true;
	}
	{
		/* code */
	}
	// 备份头节点
	LList_t *P1 = Head->next;
	LList_t *P2 = P1->next;
	while (P2->next)
	{
		// 找到data数据所在位置
		if (data == P2->data)
		{
			// 如果data所在节点是为尾节点,如果在则执行删除
			if (NULL == P2->next)
			{
				P1->next = NULL;
				free(P2);
				return true;
			}
			// 如果data所在节点不是为尾节点
			// P1链接P2的下节点
			P1 = P2->next;
			// 初始化P2的指针域
			P2->next = NULL;
			// 释放堆空间
			free(P2);
			return true;
		}
		// 偏移
		P1 = P1->next;
		P2 = P2->next;
	}
}

/********************************************************************
 *
 *	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);
	}
}

​```
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 12天前 0

暂无评论

推荐阅读
  H5oyQecqjP4R   12天前   33   0   0 C语言
  TfjjLvnv5b8J   13天前   19   0   0 C语言
6Df3JnWQUC5m