C/C++ 链表操作

struct MListNode
{
	MListNode* pNext;
};
struct MList 
{
	MListNode head;
	int nSize;
};
//比较节点
typedef bool (*CompareListNode)(MListNode*, MListNode*);
//打印节点
typedef void (*PrintListNode)(MListNode*);
//清空内存
typedef void (*ClearListNode)(MListNode*);

//初始化链表
MList* initList();
//获取大小
int getSize(MList* pList);

//按位置插入链表
bool insertNode(MList* pList,unsigned int pos, MListNode* pNode);
//尾部插入
bool push_back(MList* pList, MListNode* pNode);
//头部插入
bool push_front(MList* pList, MListNode* pNode);

//删除
bool deleteNode(MList* pList, int pos);
bool deleteNode(MList* pList, MListNode* pNode);
//尾部删除
bool pop_back(MList* pList);
//头部删除
bool pop_front(MList* pList);
//清空
void clear(MList* pList, ClearListNode clearFun);

//获取最后一个
MListNode* back(MList* pList);
//获取第一个
MListNode* front(MList* pList);
//按照位置获取
MListNode* getNode(MList* pList, int pos);
//查找
bool FindNode(MList* pList, MListNode* pNode, CompareListNode compareFun);
//打印
void PrintNode(MList* pList, PrintListNode printFun);


#include <stdlib.h>
//比较节点
//typedef bool (*CompareListNode)(MListNode*, MListNode*);
//打印节点
//typedef void (*PrintListNode)(MList*);

//初始化链表
MList* initList()
{
	//MList* pList = (MList*)malloc(sizeof(MList));
	MList* pList = new MList;
	pList->head.pNext = 0;
	pList->nSize = 0;
	return pList;
}
//获取大小
int getSize(MList* pList) 
{
	return pList->nSize;
}

//按位置插入链表
bool insertNode(MList* pList,unsigned int pos, MListNode* pNode) 
{
	if (pList == nullptr || pNode == nullptr)
	{
		return false;
	}
	if (pos > pList->nSize) 
	{
		return false;
	}
	MListNode* pNodeTail = &pList->head;
	for (size_t i = 0; i < pos; i++)
	{
		pNodeTail = pNodeTail->pNext;
	}
	pNode->pNext = pNodeTail->pNext;
	pNodeTail->pNext = pNode;
	pList->nSize++;
	return true;
}
//尾部插入
bool push_back(MList* pList, MListNode* pNode) 
{
	if (pList == nullptr || pNode == nullptr)
	{
		return false;
	}
	MListNode* pNodeTail = &pList->head;
	while (pNodeTail->pNext != nullptr)
	{
		pNodeTail = pNodeTail->pNext;
	}
	pNodeTail->pNext = pNode;
	pNode->pNext = nullptr;
	pList->nSize++;
	return true;
}
//头部插入
bool push_front(MList* pList, MListNode* pNode) 
{
	if (pList == nullptr || pNode == nullptr)
	{
		return false;
	}
	MListNode* pNodeTail = &pList->head;
	pNode->pNext = pNodeTail->pNext;
	pNodeTail->pNext = pNode;
	pList->nSize++;
	return true;
}

//删除
bool deleteNode(MList* pList, int pos)
{
	if (pList == nullptr)
	{
		return false;
	}
	if (pos >= pList->nSize)
	{
		return false;
	}
	MListNode* pNodeTail = &pList->head;
	for (int i = 0;i<pos;i++)
	{
		pNodeTail = pNodeTail->pNext;
	}
	MListNode* pNodeNext = pNodeTail->pNext->pNext;
	pNodeTail->pNext = pNodeNext;
	pList->nSize--;
	return false;
}
bool deleteNode(MList* pList, MListNode* pNode) 
{
	if (pList == nullptr || pNode == nullptr)
	{
		return false;
	}
	MListNode* pNodeTail = &pList->head;
	while (pNodeTail->pNext != nullptr)
	{
		if (pNodeTail->pNext == pNode)
		{
			pNodeTail->pNext = pNode->pNext;
			pNode->pNext = nullptr;
			pList->nSize--;
			return true;
		}
		pNodeTail = pNodeTail->pNext;
	}
	
	return true;
}
//尾部
bool pop_back(MList* pList) 
{
	if (pList == nullptr)
	{
		return false;
	}
	MListNode* pNodeTail = &pList->head;
	MListNode* pNodeLast = pNodeTail;
	while (pNodeTail->pNext != nullptr)
	{
		pNodeLast = pNodeTail;
		pNodeTail = pNodeTail->pNext;
	}
	pNodeLast->pNext = nullptr;
	pList->nSize--;
	return true;
}
//头部
bool pop_front(MList* pList) 
{
	if (pList == nullptr)
	{
		return false;
	}
	MListNode* pNodeTail = &pList->head;
	if (pNodeTail->pNext && pNodeTail->pNext->pNext)
	{
		pNodeTail->pNext = pNodeTail->pNext->pNext;
	}
	else 
	{
		pNodeTail->pNext = nullptr;
	}
	pList->nSize--;
	return true;
}
//清空
void clear(MList* pList, ClearListNode clearFun)
{
	if (pList == nullptr)
	{
		return;
	}
	
	for (size_t i = 0; i < pList->nSize; i++)
	{
		MListNode* pNode = back(pList);
		clearFun(pNode);
	}
	MListNode* pNodeTail = &pList->head;
	pNodeTail->pNext = nullptr;
	pList->nSize = 0;
}

//获取最后一个
MListNode* back(MList* pList) 
{
	if (pList == nullptr)
	{
		return nullptr;
	}
	MListNode* pNodeTail = &pList->head;
	while (pNodeTail->pNext != nullptr)
	{
		pNodeTail = pNodeTail->pNext;
	}
	return pNodeTail;
}
//获取第一个
MListNode* front(MList* pList) 
{
	if (pList == nullptr)
	{
		return nullptr;
	}
	MListNode* pNodeTail = &pList->head;
	return pNodeTail->pNext;
}
//按照位置获取
MListNode* getNode(MList* pList, int pos) 
{
	if (pList == nullptr)
	{
		return nullptr;
	}
	if (pos >= pList->nSize)
	{
		return nullptr;
	}
	MListNode* pNodeTail = pList->head.pNext;
	for (int i = 0; i < pos; i++)
	{
		pNodeTail = pNodeTail->pNext;
	}
	return pNodeTail;
}
//查找
bool FindNode(MList* pList, MListNode* pNode, CompareListNode compareFun) 
{
	if (pList == nullptr || pNode == nullptr)
	{
		return false;
	}
	MListNode* pNodeTail = &pList->head;
	while (pNodeTail->pNext != nullptr)
	{
		if (compareFun(pNodeTail->pNext, pNode))
		{
			return true;
		}
		pNodeTail = pNodeTail->pNext;
	}
	return false;
}
//打印
void PrintNode(MList* pList, PrintListNode printFun) 
{
	if (pList == nullptr)
	{
		return;
	}
	MListNode* pNodeTail = &pList->head;
	while (pNodeTail->pNext != nullptr)
	{
		printFun(pNodeTail->pNext);
		pNodeTail = pNodeTail->pNext;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BJ4015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值