单链表

不带头结点的单链表

LinkList.h

#pragma once

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

typedef int _LLDataType;

typedef struct Node
{
	_LLDataType _data;
	struct Node * next;
}Node, *pNode, *pList;

void PrintList(pList plist);
void InitList(pList* pplist);
void DestroyList(pList* pplist);
pNode BuyNode(_LLDataType data);
void PushBack(pList* pplist, _LLDataType data);
int GetListLength(pList plist);
void PopBack(pList* pplist);
void PushFront(pList* pplist, _LLDataType data);
void PopFront(pList* pplist);
pNode Find(pList plist, _LLDataType data);
void Insert(pList* pplist, pNode pos, _LLDataType data);
void Erase(pList* pplist, pNode pos);
void Remove(pList* pplist, _LLDataType data);
void RemoveAll(pList* pplist, _LLDataType data);

LinkList.c

#include"LinkList.h"

void PrintList(pList plist)
{
	pNode cur = plist;
	while (cur)
	{
		printf("%d-->>", cur->_data);
		cur = cur->next;
	}
	printf("NULL\n");
}

void InitList(pList* pplist)
{
	assert(pplist);
	*pplist = NULL;
}

void DestroyList(pList* pplist)
{
	pNode cur = NULL;
	assert(pplist);
	cur = *pplist;
	while (cur)
	{
		pNode del = cur;
		cur = cur->next;
		free(del);
		del = NULL;
	}
	*pplist = NULL;
}

pNode BuyNode(_LLDataType data)
{
	pNode newNode = (pNode)malloc(sizeof(Node));
	assert(newNode);
	newNode->next = NULL;
	newNode->_data = data;
	return newNode;
}

void PushBack(pList* pplist, _LLDataType data)
{
	pNode cur = NULL;
	assert(pplist);
	cur = *pplist;
	if (cur == NULL)
	{
		*pplist = BuyNode(data);
	}
	else
	{
		while (cur->next)
		{
			cur = cur->next;
		}
		cur->next = BuyNode(data);
	}
}

int GetListLength(pList plist)
{
	int size = 0;
	pNode cur = plist;
	while (cur)
	{
		++size;
		cur = cur->next;
	}
	return size;
}

void PopBack(pList* pplist)
{
	pNode cur = NULL;
	assert(pplist);
	cur = *pplist;
	//链表为空,直接返回
	if (cur == NULL)
	{
		return;
	}
	//链表有一个节点,删除置空
	if (cur->next == NULL)
	{
		free(cur);
		cur = NULL;
		return;
	}
	//链表有多个节点,遍历找最后一个节点删除
	while (cur->next->next)
	{
		cur = cur->next;
	}
	free(cur->next);
	cur->next = NULL;
}

void PushFront(pList* pplist, _LLDataType data)
{
	pNode newNode = NULL;
	assert(pplist);
	newNode = BuyNode(data);
	newNode->next = *pplist;
	*pplist = newNode;
}

void PopFront(pList* pplist)
{
	pNode del = NULL;
	assert(pplist);
	del = *pplist;
	if (del == NULL)
	{
		return;
	}
	*pplist = del->next;
	free(del);
	del = NULL;
}

pNode Find(pList plist, _LLDataType data)
{
	pNode cur = plist;
	while (cur)
	{
		if (cur->_data == data)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

void Insert(pList* pplist, pNode pos, _LLDataType data)
{
	pNode newNode = BuyNode(data);
	pNode cur = NULL;
	assert(pos);
	assert(pplist);
	cur = *pplist;
	if (cur == pos)
	{
		newNode->next = cur;
		*pplist = newNode;
		return;
	}
	else
	{
		while (cur && cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = newNode;
		newNode->next = pos;
	}
}

void Erase(pList* pplist, pNode pos)
{
	pNode cur = NULL;
	assert(pplist);
	assert(pos);
	cur = *pplist;
	if (cur == NULL)
	{
		return;
	}
	if (cur == pos)
	{
		pNode del = cur;
		*pplist = del->next;
		free(del);
		del = 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, _LLDataType data)
{
	pNode cur = NULL;
	pNode prev = NULL;
	assert(pplist);
	cur = *pplist;
	while (cur)
	{
		if (cur->_data == data)
		{
			if (cur == *pplist)
			{
				PopFront(pplist);
			}
			else
			{
				prev->next = cur->next;
				free(cur);
				cur = NULL;
			}
			return;
		}
		else
		{
			prev = cur;
			cur = cur->next;
		}
	}
}

void RemoveAll(pList* pplist, _LLDataType data)
{
	assert(pplist);
	pNode cur = NULL;
	pNode prev = NULL;
	assert(pplist);
	cur = *pplist;
	while (cur)
	{
		if (cur->_data == data)
		{
			if (cur == *pplist)
			{
				PopFront(pplist);
				cur = *pplist;
			}
			else
			{
				prev->next = cur->next;
				free(cur);
				cur = NULL;
			}
		}
		else
		{
			prev = cur;
			cur = cur->next;
		}
	}
}

Test.c

#include"LinkList.h"

void testLinkList()
{
	pList list;
	pNode pos = NULL;

	InitList(&list);

	PushBack(&list, 1);
	PushBack(&list, 3);
	PushBack(&list, 7);
	PushBack(&list, 2);
	PushBack(&list, 0);
	PushBack(&list, 7);
	PushBack(&list, 3);
	PushBack(&list, 1);
	PushBack(&list, 9);
	PushBack(&list, 0);
	PushBack(&list, 2);

	PrintList(list);
	printf("链表长度-->> %d\n", GetListLength(list));

	PopBack(&list);
	PrintList(list);

	PushFront(&list, 9);
	PrintList(list);

	PopFront(&list);
	PrintList(list);

	pos = Find(list, 1);
	printf("%d\n", *pos);

	Insert(&list, pos, 2);
	PrintList(list);

	Erase(&list, pos);
	PrintList(list);

	Remove(&list, 0);
	PrintList(list);

	RemoveAll(&list, 2);
	PrintList(list);

	DestroyList(&list);
	PrintList(list);
}

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值