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;
}
}
C/C++ 链表操作
于 2024-03-11 17:36:19 首次发布