版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hansionz/article/details/82356787
0.定义结构
typedef int DataType;
//链表结点
typedef struct Node
{
DataType data;
struct Node * next;
}Node,*pNode,LinkList,*pLinkList;
1.定义接口
/初始化链表
void InitLinkList(pLinkList *ppl);
//尾插
void PushBack(pLinkList *ppl, DataType data);
//尾删
void PopBack(pLinkList* ppl);
//打印链表
void PrintLinkList(pLinkList *ppl);
//寻找某一节点
pNode Find(pLinkList plist, DataType data);
//在指定位置前插入结点
void InsertNodeNotHead(pLinkList *ppl, DataType data, pNode pos);
//头插
void PushFront(pLinkList * ppl, DataType data);
//头删
void PopFront(pLinkList* ppl);
//在指定位置前插入结点
void Insert(pLinkList* ppl, pNode pos, DataType data);
//在指定位置删除结点
void Erase(pLinkList* ppl, pNode pos);
//删除指定数据的第一个结点
void Remove(pLinkList* ppl, DataType data);
//删除指定数据的所有结点
void RemoveAll(pLinkList* ppl, DataType data);
//求链表的长度
int GetListLength(pLinkList pl);
//销毁链表
void DestoryList(pLinkList *ppl);
2.函数实现
void InitLinkList(pLinkList * ppl)
{
assert(ppl != NULL);
*ppl = NULL;
}
pNode BuyNode(DataType data)
{
pNode tmp = (pNode)malloc(sizeof(Node));
if (tmp == NULL)
{
perror("use malloc");
return NULL;
}
tmp->data = data;
tmp->next = NULL;
return tmp;
}
void PushBack(pLinkList *ppl, DataType data)
{
assert(ppl != NULL);
pNode newNode = BuyNode(data);
if (newNode == NULL)
{
exit(EXIT_FAILURE);
}
if (*ppl == NULL)//空链表,直接插入
{
(*ppl)= newNode;
}
else//非空链表,先找到最后一个节点在插入
{
pNode cur = *ppl;
while ((cur->next)!=NULL)
{
cur = cur->next;
}
cur->next = newNode;
}
}
void PopBack(pLinkList* ppl)
{
assert(ppl != NULL);
if (*ppl == NULL)
{
return;
}
if ((*ppl)->next == NULL)
{
free(*ppl);
ppl = NULL;
}
else
{
pNode cur = *ppl;
while (cur->next->next != NULL)
{
cur = cur->next;
}
free(cur->next);
cur->next = NULL;
}
}
void PushFront(pLinkList* ppl, DataType data)
{
assert(ppl != NULL);
pNode newNode = BuyNode(data);
newNode->next = *ppl;
*ppl = newNode;
}
void PopFront(pLinkList* ppl)
{
assert(ppl != NULL);
if (*ppl == NULL)
{
return;
}
else
{
pNode del = *ppl;
*ppl = del->next;
free(del);
del = NULL;
}
}
void PrintLinkList(pLinkList *ppl)
{
assert(ppl != NULL);
pNode cur = *ppl;
while (cur != NULL)
{
printf("%d--->", cur->data);
cur = cur->next;
}
printf("over\n");
}
void DestoryList(pLinkList *ppl)
{
assert(ppl != NULL);
pNode cur = *ppl;
while (cur != NULL)
{
pNode del = cur;
cur = cur->next;
free(del);
del = NULL;
}
*ppl = NULL;
}
void Insert(pLinkList* ppl, pNode pos, DataType data)
{
assert(ppl != NULL);
assert(pos != NULL);
assert(*ppl != NULL);
pNode NewNode = BuyNode(data);
if (*ppl == pos)
{
//前插
NewNode->next = *ppl;
*ppl = NewNode;
}
else
{
pNode cur = *ppl;
while (cur&&cur->next != pos)
{
cur = cur->next;
}
NewNode->next = cur->next;
cur->next = NewNode;
}
}
void Erase(pLinkList* ppl, pNode pos)
{
pNode cur = *ppl;
assert(*ppl != NULL);
assert(pos != NULL);
assert(*ppl != NULL);
if (*ppl == pos)
{
*ppl = pos->next;
free(pos);
pos = NULL;
}
else
{
while (cur&&cur->next != pos)
{
cur = cur->next;
}
cur->next = pos->next;
free(pos);
pos = NULL;
}
}
void Remove(pLinkList* ppl, DataType data)
{
pNode cur = *ppl;
pNode pre = *ppl;
assert(ppl != NULL);
while (cur)
{
if (cur->data == data)
{
if (*ppl == cur)
{
*ppl = 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(pLinkList* ppl, DataType data)
{
pNode cur = *ppl;
pNode pre = *ppl;
pNode del = NULL;
assert(ppl != NULL);
while (cur)
{
if (cur->data == data)
{
if (*ppl == cur)
{
*ppl = cur->next;
free(cur);
cur = *ppl;
}
else
{
del = cur;
pre->next = cur->next;
cur = cur->next;
free(del);
del = NULL;
}
}
else
{
pre = cur;
cur = cur->next;
}
}
}
pNode Find(pLinkList plist, DataType data)
{
pNode cur = plist;
if (plist == NULL)
{
return NULL;
}
while (cur)
{
if (cur->data == data)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
int GetListLength(pLinkList pl)
{
int count = 0;
pNode cur = pl;
while (cur)
{
count++;
cur = cur->next;
}
return count;
}