代码实现链表的操作函数
// 值类型
typedef int DataType;
typedef struct SListNode {
DataType data; // 值
struct ListNode *pNext; // 指向下一个结点
} SListNode;
// 初始化
void SListInit(SListNode **ppFirst);
// 尾部插入
void SListPushBack(SListNode** ppFirst, DataType data);
// 头部插入
void SListPushFront(SListNode **ppFirst, DataType data);
// 尾部删除
void SListPopBack(SListNode **ppFirst);
// 头部删除
void SListPopFront(SListNode **ppFirst);
// 给定结点插入,插入到结点前
void SListInsert(SListNode **ppFirst, SListNode *pPos, DataType data);
// 给定结点删除
void SListErase(SListNode **ppFirst, SListNode *pPos);
// 按值删除,只删遇到的第一个
void SListRemove(SListNode **ppFirst, DataType data);
// 按值删除,删除所有的
void SListRemoveAll(SListNode **ppFirst, DataType data);
// 销毁
void SListDestroy(SListNode **ppFirst);
// 按值查找,返回第一个找到的结点指针,如果没找到,返回 NULL
int SListFind(SListNode *pFirst, DataType data);
单链表:
Slist.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
typedef int DataType;
typedef struct Slist
{
DataType data;
struct Slist *pNext;
}Slist;
void TestSlist();
void Init(Slist **ppHead);
void pushBack(Slist **ppHead, DataType data);
void pushFornt(Slist **ppHead, DataType data);
void popBack(Slist **ppHead);
void popFornt(Slist **ppHead);
void Insert(Slist **ppHead, Slist *pPosNode, DataType data);
void Erase(Slist **ppHead, Slist *pPosNode);
int Size(Slist **ppHead);
Slist *Find(Slist *pHead, DataType data);
void Remove(Slist **ppHead, DataType data);
void RemoveAll(Slist **ppHead, DataType data);
void Destory(Slist **ppHead);
void print(Slist *pHead);
main.c
#include"Slist.h"
int main()
{
TestSlist();
system("pause");
return 0;
}
Slist.c
#include"Slist.h"
void Init(Slist **ppHead)//初始化
{
*ppHead = NULL;
}
static Slist * BuyNewNode(DataType data)//创建新结点
{
Slist *pNewNode = (Slist *)malloc(sizeof(Slist));
assert(pNewNode);
pNewNode->data = data;
pNewNode->pNext = NULL;
return pNewNode;
}
void pushBack(Slist **ppHead, DataType data)//尾插
{
Slist *pNewNode = BuyNewNode(data); //1.要有空间
if (*ppHead == NULL) //2.把数据装进去
{ //3.把空间的结点连接上去
*ppHead = pNewNode; //a.把链子拆开(找到对地方)
return; //b.把上半段的Next连接到新的结点上
} //c.把新的结点的Next连接到原来的下半段
//**千万不要把指针丢掉了。
Slist *pNode = *ppHead;
while (pNode->pNext != NULL)
{
pNode = pNode->pNext;
}
pNewNode->pNext = pNode->pNext;
pNode->pNext = pNewNode;
}
void pushFornt(Slist **ppHead, DataType data)//头插
{
Slist *pNewNode = BuyNewNode(data);
pNewNode->pNext = *ppHead;
*ppHead = pNewNode;
}
void popBack(Slist **ppHead) //尾删
{
if(*ppHead == NULL) //1.拆链子(找对的地方)
{
printf("链表已经为空\n"); //2.把空间释放掉
return;
} //3.把上半段连接到下半段
if ((*ppHead)->pNext == NULL)
{
free(*ppHead);
*ppHead = NULL;
return;
}
Slist *pNext, *pNode;
pNode = *ppHead;
while (pNode->pNext->pNext != NULL)
{
pNode = pNode->pNext;
}
pNext = pNode->pNext;
pNode->pNext = NULL;
free(pNext);
}
void popFornt(Slist **ppHead)//头删
{
if (*ppHead == NULL)
{
printf("链表已经为空\n");
return;
}
Slist *pNode = *ppHead;
Slist *pNext = pNode->pNext;
free(pNode);
*ppHead = pNext;
}
void Insert(Slist **ppHead, Slist *pPosNode, DataType data)//任意位置插入
{
if (*ppHead == pPosNode)
{
pushFornt(ppHead, data);
return;
}
Slist *pNode; //1.找链子
for (pNode = *ppHead; pNode->pNext != pPosNode; pNode = pNode->pNext) //2.Byu结点
{ //3.把新的结点挂在链子上
}
Slist *pNewNode = BuyNewNode(data);
pNewNode->pNext = pPosNode;
pNode->pNext = pNewNode;
}
void Erase(Slist **ppHead, Slist *pPosNode)//任意位置删除
{
if (pPosNode == *ppHead)
{
popFornt(ppHead);
return;
}
Slist *pNode;
for (pNode = *ppHead; pNode->pNext != pPosNode; pNode = pNode->pNext)
{ }
pNode->pNext = pNode->pNext->pNext;
free(pPosNode);
}
int Size(Slist **ppHead)//统计链表中一共有多少个数
{
int size = 0;
Slist *pNode;
for (pNode = *ppHead; pNode != NULL; pNode = pNode->pNext)
{
size++;
}
return size;
}
Slist *Find(Slist *pHead, DataType data)//在链表中查找数
{
Slist *pNode;
for (pNode = pHead; pNode != NULL; pNode = pNode->pNext)
{
if (pNode->data == data)
{
return pNode;
}
}
return NULL;
}
void Remove(Slist **ppHead, DataType data)//删除链表中第一次出现的数
{
Slist *pPosNode=Find(*ppHead, data);
if (pPosNode != NULL)
{
Erase(ppHead, pPosNode);
}
}
void RemoveAll(Slist **ppHead, DataType data)//删除链表中所有出现的数
{
Slist *pNode = *ppHead;
Slist *pDel;
while (pNode->pNext != NULL)
{
if (pNode->pNext->data == data)
{
pDel = pNode->pNext;
pNode->pNext = pDel->pNext;
free(pDel);
}
else
{
pNode = pNode->pNext;
}
}
if ((*ppHead)->data == data)
{
popFornt(ppHead);
}
}
void print(Slist *pHead)//打印链表
{
Slist *pNode;
for (pNode = pHead; pNode != NULL; pNode =pNode->pNext)
{
printf("%2d->", pNode->data);
}
printf("NULL\n");
}
void Destory(Slist **ppHead)//销毁链表
{
Slist *pNode,*pNext;
for (pNode = *ppHead; pNode != NULL; pNode = pNext)
{
pNext = pNode->pNext;
free(pNode);
}
*ppHead = NULL;
}
void TestSlist()
{
Slist *pHead;
Init(&pHead);
pushBack(&pHead, 1);
pushBack(&pHead, 2);
pushBack(&pHead, 3);
pushBack(&pHead, 4);
print(pHead);
popFornt(&pHead);
popFornt(&pHead);
popFornt(&pHead);
popFornt(&pHead);
print(pHead);
pushFornt(&pHead, 1);
pushFornt(&pHead, 2);
pushFornt(&pHead, 3);
pushFornt(&pHead, 4);
print(pHead);
popBack(&pHead);
popBack(&pHead);
popBack(&pHead);
popBack(&pHead);
print(pHead);
pushFornt(&pHead, 1);
pushFornt(&pHead, 2);
pushFornt(&pHead, 3);
pushFornt(&pHead, 4);
print(pHead);
Insert(&pHead, pHead->pNext->pNext, 5);
print(pHead);
Erase(&pHead, pHead->pNext->pNext);
print(pHead);
int ret = Size(&pHead);
printf("%d\n", ret);
Remove(&pHead, 1);
print(pHead);
pushFornt(&pHead, 1);
pushFornt(&pHead, 2);
pushFornt(&pHead, 2);
pushFornt(&pHead, 4);
print(pHead);
RemoveAll(&pHead, 2);
print(pHead);
Destory(&pHead);
print(pHead);
}