C语言实现链表的基本操作(增,删,查,改等等)

链表是一种物理存储单元上非连续、非顺序的存储结构数据元素的逻辑顺序是通过链表中的指针链接次序实现的。

链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。

每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

LinkList.h

#ifndef __LINKLIST_H__ 
#define __LINKLIST_H__ 

#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <stdlib.h>

typedef int DataType;
typedef struct Node
{
	DataType data;
	struct Node* next;
}Node, *pNode, List, *pList;


//初始化链表
void InitLinkList(pList* pplist);
//创建节点
pNode BuyNode(DataType d);
//销毁链表
void DestroyLinkList(pList* pplist);
//尾部插入一个元素
void PushBack(pList* pplist, DataType d);
//尾部删除一个元素
void PopBack(pList* pplist);
//头部插人一个元素
void PushFront(pList* pplist, DataType d);
//头部删除一个元素
void PopFront(pList* pplist);
//找一个元素
pNode Find(pList plist, DataType d);

//在指定位置之前插入一个值 
void Insert(pList* pplist, pNode pos, DataType d);
//指定位置删除 
void Erase(pList* pplist, pNode pos);
//删除指定元素
void Remove(pList* pplist, DataType d);
//删除所有指定元素
void RemoveAll(pList* pplist, DataType d);
//打印链表
void PrintLinkList(pList *pplist);
//链表的长度
int GetListLength(pList pplist);

#endif //__LINKLIST_H__ 

LinkList.c

#include "LinkList.h"


//初始化链表
void InitLinkList(pList* pplist)
{
	assert(pplist);
	*pplist = NULL;
}
//创建节点
pNode BuyNode(DataType d)
{
	pNode node = (pNode)malloc(sizeof(Node));
	if (node == NULL)
	{
		return NULL;
	}
	node->data = d;
	node->next = NULL;
	return node;

}

//销毁链表
void DestroyLinkList(pList* pplist)
{
	assert(pplist);
	pNode cur = *pplist;
	while (cur)
	{
		pNode del = cur;
		cur = cur->next;
		free(del);
		del = NULL;
	}
	*pplist = NULL;
}
//尾插
void PushBack(pList* pplist, DataType d)
{
	assert(pplist);
	pNode newNode = BuyNode(d);
	if (newNode == NULL)
	{
		return;
	}
	if (*pplist == NULL)
	{
		*pplist = newNode;
	}
	else
	{
		pNode cur = *pplist;
		while (cur->next)
		{
			cur = cur->next;
		}
		cur->next = newNode;
	}
}
//尾删
void PopBack(pList* pplist)
{
	assert(pplist);
	pNode cur = *pplist;
	if (cur == NULL)
	{
		return;
	}
	if (cur->next == NULL)
	{
		free(cur);
		cur = NULL;
	}
	else
	{
		while (cur->next->next!=NULL)
		{
			cur = cur->next;
		}
		free(cur->next);
		cur->next = NULL;
	}
}
//头插
void PushFront(pList* pplist, DataType d)
{
	assert(pplist);
	pNode newNode = BuyNode(d);
	if (newNode == NULL)
	{
		return;
	}
	newNode->next = *pplist;
	*pplist = newNode;
}
//头删
void PopFront(pList* pplist)
{
	assert(pplist);
	if (*pplist == NULL)
	{
		return;
	}
	else
	{
		pNode cur = *pplist;
		*pplist = cur->next;
		free(cur);
		cur = NULL;
	}
}

//在链表中找一个元素,返回节点的位置
pNode Find(pList plist, DataType d)
{
	pNode cur = plist;
	if (plist == NULL)
	{
		return;
	}
	while (cur)
	{
		if (cur->data == d)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

//在指定位置之前插入一个值 
void Insert(pList* pplist, pNode pos, DataType d)
{
	assert(pplist);
	assert(pos);
	pNode newNode = BuyNode(d);
	if (*pplist == pos)
	{
		//前插
		newNode->next = *pplist;
		*pplist = newNode;
	}
	else
	{
		pNode cur = *pplist;
		while (cur&&cur->next != pos)
		{
			cur = cur->next;
		}
		if (cur != NULL)
		{
			newNode->next = cur->next;
			cur->next = newNode;
		}
	}
}
//指定位置删除 
void Erase(pList* pplist, pNode pos)
{
	assert(pplist);
	assert(pos);
	pNode cur = *pplist;
	if (*pplist == pos)
	{
		*pplist = pos->next;
		free(pos);
		pos = NULL;
	}
	else
	{
		while (cur && cur->next != pos)
		{
			cur = cur->next;
		}
		if (cur != NULL)
		{
			cur->next = pos->next;
			free(pos);
			pos = NULL;
		}
	}
}
//删除指定元素
void Remove(pList* pplist, DataType d)
{
	assert(pplist);
	pNode cur = *pplist;
	pNode pre = *pplist;
	while (cur)
	{
		if (cur->data == d)
		{
			if (*pplist == cur)
			{
				*pplist = cur->next;
				free(cur);
				cur = NULL;
			}
			else
			{
				while (pre&&pre->next != cur)
				{
					pre = pre->next;
				}
				pre->next = cur->next;
				free(cur);
				cur = NULL;
			}
			return;
		}
		cur = cur->next;
	}

	
}
//删除所有指定元素
void RemoveAll(pList* pplist, DataType d)
{
	assert(pplist);
	pNode cur = *pplist;
	pNode pre = *pplist;
	pNode del = NULL;
	while (cur)
	{
		if (cur->data == d)
		{
			if (*pplist == cur)
			{
				*pplist = cur->next;
				free(cur);
				cur = *pplist;
			}
			else
			{
				del = cur;
				pre->next = cur->next;
				cur = cur->next;
				free(del);
				del = NULL;
			}
		}
		else
		{
			pre = cur;
			cur = cur->next;
		}
	}
}

//求链表的长度
int GetListLength(pList plist)
{
	int count = 0;
	pNode cur = plist;
	while (cur)
	{
		count++;
		cur = cur->next;
	}
	return count;
}
//打印链表
void PrintLinkList(pList *pplist)
{
	assert(pplist);
	pNode cur = *pplist;
	while (cur)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}

test.c

#include"LinkList.h"

void TestLinklist()
{
	pList plist;
	InitLinkList(&plist);
	PushBack(&plist, 1);
	PushBack(&plist, 1);

	PushBack(&plist, 2);
	PushBack(&plist, 3);
	PushBack(&plist, 4);
	PrintLinkList(&plist);

	PopBack(&plist);
	PrintLinkList(&plist);

	PushFront(&plist, 5);
	PrintLinkList(&plist);

	PopFront(&plist);
	PrintLinkList(&plist);

	Remove(&plist, 2);
	PrintLinkList(&plist);

	RemoveAll(&plist, 1);
	PrintLinkList(&plist);

}

int main()
{
	TestLinklist();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Damn_Yang/article/details/83689944