数据结构和算法C语言实现:链表的实现(基于动态内存分配)

1、链表是由称为节点的元素组成的序列。

      每个节点包含两部分:1)、data(数据)部分,存放列表的一个元素。 2)next 部分,存放一个指针,指出包含下一列表元素的节点的位置。如果没有下一个元素,则使用一个特殊的空值。

2、链表的基本操作包括构造(创建)、插入节点,删除节点,修改节点。而实现这些的基本操作便是对链表的遍历。

3、为了实现链表的遍历,我们需要一个辅助指针。设为ptr,ptr指向链表的头结点(pHead)。

 while(NULL != ptr)
{
     ptr = ptr->pNext;
     ......//do something
}

4、节点的插入。

       插入的方式有多种,可以在某个位置插入,可以在末尾插入,可以在头部插入。这个可以根据自己的喜好来定。我在这假定在某个元素的前面插入。插入元素需要一个辅助指针来遍历。

int List_Insert(struct Student *pHead,char *pName)
{
	int ret = 0;
	char szTemp[32] = { 0 };
	int nAge = 0;
	struct Student *pTemp = NULL;
	struct Student *pCurrent = NULL;
	if (NULL == pHead||NULL==pName)
	{
		ret = -2;
		printf("Func_List_Insetr_Error::%d", ret);
		return ret;
	}

	printf("please input the name you want to insert:");
	scanf_s("%s", szTemp, 32);
	printf("please input the age you want to insert:");
	scanf_s("%d", &nAge, sizeof(nAge));

	pCurrent = pHead;
	pCurrent = pCurrent->pNext;

	while (NULL != pCurrent->pNext)
	{
		if (strcmp(pCurrent->szName, pName) == 0)
		{
			pTemp = (struct Student*)malloc(sizeof(struct Student));
			if (NULL == pTemp)
			{
				ret = -2;
				printf("Func_List_Insetr_Error::%d", ret);
				return ret;
			}

			pTemp->szName = (char*)malloc(32 * sizeof(char));

			

			strcpy_s(pTemp->szName, strlen(szTemp) + 1, szTemp);
			pTemp->age = nAge;

			pTemp->pNext = pCurrent->pNext;
			pCurrent->pNext = pTemp;

			return ret;
		}

		pCurrent = pCurrent->pNext;
	}

	pTemp = (struct Student*)malloc(sizeof(struct Student));
	if (NULL == pTemp)
	{
		ret = -2;
		printf("Func_List_Insetr_Error::%d", ret);
		return ret;
	}
	pTemp->szName = (char*)malloc(32 * sizeof(char));

	strcpy_s(pTemp->szName, strlen(szTemp) + 1, szTemp);
	pTemp->age = nAge;
	pTemp->pNext = NULL;

	pCurrent->pNext = pTemp;
	printf("Listed_In_The_End");
	return ret;
}

  如果找到固定元素,就在其前面插入,如果没有找到,就在头部节点插入。


5、节点的删除

       删除一个节点需要遍历链表,找到对应的元素,然后将其删除。

int List_Delete(struct Student *pHead, char *pName)
{
	int ret = 0;
	struct Student *pTemp = NULL;
	struct Student *pCurrent = NULL;
	struct Student *pPrior = NULL;
	if (NULL == pHead || NULL == pName)
	{
		ret = -3;
		printf("Func_List_Delete_Error::%d", ret);
		return ret;
	}

	pPrior = pHead;
	while (NULL != pPrior)
	{
		pCurrent = pPrior->pNext;
		if (strcmp(pCurrent->szName, pName)==0)
		{
			pTemp = pCurrent;
			pPrior->pNext = pTemp->pNext;

			if (NULL != pTemp)
			{
				if (NULL != pTemp->szName)
				{
					free(pTemp->szName);
					pTemp->szName = NULL;
				}
				free(pTemp);
				pTemp = NULL;

				return ret;
			}
		}
		pPrior = pPrior->pNext;
	}

	printf("Can not Find The Relevant Point\n");
	return -1;
}
遍历链表,找到对应的元素,然后删除


          

6、最后附上源代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

 struct Student{
	char *szName;
	int age;
	struct  Student *pNext;
};

struct  Student *List_Create();
int List_Add(struct  Student *pHead);
int List_Insert(struct  Student *pHead,char *pName);
int List_Delete(struct  Student *pHead, char *pName);
void List_Print(struct  Student *pHead);
int  List_Reverse(struct Student *pHead);
void List_Destroy(struct Student *pHead);

void main()
{
	int num=0;
	int ret = 0;
	char szName[32] = { 0 };
	struct Student *pStu = List_Create();
	if (NULL == pStu)
	{
		printf("Main_Func_Error");
		return;
	}

	printf("Please Enter your Chioce:\n0(Exit)\n1(List_Add)\n2(List_Insert)\n3(List_Delete)\n4(List_Print)\n5(List_Reverse)\n:");
	scanf_s("%d", &num, sizeof(num));

	while (num)
	{
		switch (num)
		{
		case 1:
			ret=List_Add(pStu);
			if (0 != ret)
			{
				return;
			}
			break;
		case 2:
			printf("Please Input The Place You Want To Insert:");
			scanf_s("%s", szName, sizeof(szName));
			ret=List_Insert(pStu, szName);
			if (ret != 0)
			{
				return;
			}
			break;
		case 3:
			printf("Please Input The Place You Want To Delete:");
			scanf_s("%s", szName, sizeof(szName));
			ret = List_Delete(pStu, szName);
			if (ret != 0)
			{
				return;
			}
			break;
		case 4:
			List_Print(pStu);
			break;
		case 5:
			ret=List_Reverse(pStu);
			if (ret != 0)
			{
				return;
			}
			break;
		}

		printf("Please Enter your Chioce:\n0(Exit)\n1(List_Add)\n2(List_Insert)\n3(List_Delete)\n4(List_Print)\n5(List_Reverse)\n:");
		scanf_s("%d", &num, sizeof(num));
	}

	List_Destroy(pStu);
	
	system("pause");
}

struct Student *List_Create()
{
	struct Student* pHead = NULL;
	pHead = (struct Student*)malloc(sizeof(struct Student));
	if (NULL == pHead)
	{
		printf("List_struct Student_Failed::Func_List_Create_Failed");
		return NULL;
	}

	pHead->szName = (char*)malloc(32 * sizeof(char));
	if (NULL == pHead->szName)
	{
		return NULL;
	}
	memset(pHead, 0, sizeof(struct Student));

	pHead->pNext = NULL;

	return pHead;
}

int List_Add(struct Student *pHead)
{
	int ret = 0;
	char szTemp[32] = { 0 };
	int nAge = 0;
	struct Student *pTemp = NULL;
	if (NULL == pHead)
	{
		ret = -1;
		printf("Func_List_Add_Failed::%d", ret);
		return ret;
	}

	struct Student *pCurrent = NULL;
	pCurrent = pHead;
	while(NULL != (pCurrent->pNext))
	{
		pCurrent = pCurrent->pNext;
	}

	pTemp = (struct Student *)malloc(sizeof(struct Student));

	if (NULL == pTemp)
	{
		ret = -1;
		printf("Func_List_Add_Failed::%d", ret);
		return ret;
	}
	pTemp->szName = (char *)malloc(32 * sizeof(char));

	printf("please input  name:");
	scanf_s("%s", szTemp, 32);
	printf("please input  nAge:");
	scanf_s("%d", &nAge, sizeof(nAge));

	strcpy_s(pTemp->szName, strlen(szTemp) + 1, szTemp);
	pTemp->age = nAge;
	pTemp->pNext = NULL;

	pCurrent->pNext = pTemp;

	return ret;
}

int List_Insert(struct Student *pHead,char *pName)
{
	int ret = 0;
	char szTemp[32] = { 0 };
	int nAge = 0;
	struct Student *pTemp = NULL;
	struct Student *pCurrent = NULL;
	if (NULL == pHead||NULL==pName)
	{
		ret = -2;
		printf("Func_List_Insetr_Error::%d", ret);
		return ret;
	}

	printf("please input the name you want to insert:");
	scanf_s("%s", szTemp, 32);
	printf("please input the age you want to insert:");
	scanf_s("%d", &nAge, sizeof(nAge));

	pCurrent = pHead;
	pCurrent = pCurrent->pNext;

	while (NULL != pCurrent->pNext)
	{
		if (strcmp(pCurrent->szName, pName) == 0)
		{
			pTemp = (struct Student*)malloc(sizeof(struct Student));
			if (NULL == pTemp)
			{
				ret = -2;
				printf("Func_List_Insetr_Error::%d", ret);
				return ret;
			}

			pTemp->szName = (char*)malloc(32 * sizeof(char));

			

			strcpy_s(pTemp->szName, strlen(szTemp) + 1, szTemp);
			pTemp->age = nAge;

			pTemp->pNext = pCurrent->pNext;
			pCurrent->pNext = pTemp;

			return ret;
		}

		pCurrent = pCurrent->pNext;
	}

	pTemp = (struct Student*)malloc(sizeof(struct Student));
	if (NULL == pTemp)
	{
		ret = -2;
		printf("Func_List_Insetr_Error::%d", ret);
		return ret;
	}
	pTemp->szName = (char*)malloc(32 * sizeof(char));

	strcpy_s(pTemp->szName, strlen(szTemp) + 1, szTemp);
	pTemp->age = nAge;
	pTemp->pNext = NULL;

	pCurrent->pNext = pTemp;
	printf("Listed_In_The_End");
	return ret;
}

int List_Delete(struct Student *pHead, char *pName)
{
	int ret = 0;
	struct Student *pTemp = NULL;
	struct Student *pCurrent = NULL;
	struct Student *pPrior = NULL;
	if (NULL == pHead || NULL == pName)
	{
		ret = -3;
		printf("Func_List_Delete_Error::%d", ret);
		return ret;
	}

	pPrior = pHead;
	while (NULL != pPrior)
	{
		pCurrent = pPrior->pNext;
		if (strcmp(pCurrent->szName, pName)==0)
		{
			pTemp = pCurrent;
			pPrior->pNext = pTemp->pNext;

			if (NULL != pTemp)
			{
				if (NULL != pTemp->szName)
				{
					free(pTemp->szName);
					pTemp->szName = NULL;
				}
				free(pTemp);
				pTemp = NULL;

				return ret;
			}
		}
		pPrior = pPrior->pNext;
	}

	printf("Can not Find The Relevant Point\n");
	return -1;
}

void List_Print(struct Student *pHead)
{
	struct Student *pCurrent = NULL;
	int i = 1;
	if (NULL == pHead)
	{
		printf("Func_List_Print_Error");
		return;
	}
	
	pCurrent = pHead->pNext;

	while (pCurrent)
	{
		printf("The %dth pitch point szName=%s,age=%d\n", i, pCurrent->szName, pCurrent->age);
		i++;
		pCurrent = pCurrent->pNext;
	}
}

int  List_Reverse(struct Student *pHead)
{
	int ret = 0;
	struct Student *pCurrent = NULL;
	struct Student *pEnd = NULL;
	struct Student *pTemp = NULL;
	struct Student *pNew = NULL;
	if (NULL == pHead)
	{
		ret = -4;
		printf("Func_List_Reverse_Error::%d", ret);
		return ret;
	}

	pEnd = pHead->pNext;
	if (NULL == pEnd || NULL == pEnd->pNext)
	{
		return ret;
	}

	pCurrent = pEnd->pNext;
	pNew = pEnd;
	pEnd->pNext = NULL;
	do 
	{
		pTemp = pCurrent->pNext;
		pCurrent->pNext = pNew;
	
		pNew = pCurrent;
		pCurrent = pTemp;
	} while (pCurrent);

	pHead->pNext = pNew;

	return 0;
}

void List_Destroy(struct Student *pHead)
{
	struct Student *pCurrent = NULL;
	struct Student *pTemp = NULL;
	if (NULL == pHead)
	{
		return;
	}
	
	pCurrent = pHead->pNext;

	{
		free(pHead->szName);
		pHead->szName = NULL;
	}
	free(pHead);
	pHead = NULL;

	while (pCurrent)
	{
		pTemp = pCurrent;
		pCurrent = pCurrent->pNext;
		if (NULL != pTemp)
		{
			free(pTemp->szName);
			pTemp->szName = NULL;
		}
		free(pTemp);
		pTemp = NULL;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值