先来说一下链表的基本操作有哪些?
增删查。
下面是头文件中的声明:
#pragma once
typedef int Datatype;
typedef struct SList
{
Datatype data;
struct SList *pNext;
}SList;
//初始化
void SListInit(SList *pHead);
//尾插
void SListPushBack(SList* *ppHead, Datatype data);
//头插
void SListPushFront(SList* *ppHead, Datatype data);
//尾删
void SListPopBack(SList* *ppHead);
//头删
void SListPopFront(SList* *ppHead);
//任意插入
void SListInsert(SList* *ppHead, SList* pPos, Datatype data);
//任意删除
void SListErase(SList* *ppHead, SList* pPos);
// 按值删除,只删遇到的第一个
void SListRemove(SList* *ppHead, Datatype data);
// 按值删除,删除所有的
void SListRemoveAll(SList* *ppHead, Datatype data);
//打印链表
void SListPrint(SList* pHead);
//判断是否为空,1 表示空, 0 表示不空
int SListEmpty(SList* *ppHead);
//按值查找,返回找到的结点
SList* SListFind(SList* *ppHead,Datatype data);
//链表个数
Datatype SListCount(SList* *ppHead);
//销毁
void SListDestory(SList* *phead);
这是主要的实现函数:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#include<math.h>
#include"SList.h"
//初始化链表
void SListInit(SList *pHead)
{
pHead = NULL;
}
//销毁链表
void SListDestory(SList* *ppHead)
{
SList* pNode = *ppHead;
SList* pSave = NULL;
if (pNode == NULL)
return;
while (pNode != NULL)
{
pSave = pNode->pNext;
free(pNode);
pNode = NULL;
}
*ppHead = NULL;
}
//打印链表
void SListPrint(SList* pHead)
{
SList *pNode = pHead;
while (pNode != NULL)
{
printf("%d->", pNode->data);
pNode = pNode->pNext;
}
printf("NULL\n");
}
//创建一个新节点
static SList* BuyNewnode(Datatype data)
{
SList*pNewnode = (SList*)malloc(sizeof(SList));
assert(pNewnode);
pNewnode->data = data;
return pNewnode;
}
//尾插
void SListPushBack(SList* *ppHead, Datatype data)
{
SList* pNode = *ppHead;
SList* pNewnode = BuyNewnode(data);
if (ppHead == NULL)
return;
if (*ppHead == NULL)
*ppHead = pNewnode;
//特殊情况处理,如果链表为空的话
if (pNode == NULL)
{
pNode = pNewnode;
pNode->pNext = NULL;
*ppHead = pNode;
return;
}
//正常情况处理
while (pNode->pNext != NULL)
pNode = pNode->pNext;
pNewnode->pNext = pNode->pNext;
pNode->pNext = pNewnode;
}
//头插
void SListPushFront(SList* *ppHead, Datatype data)
{
SList* pNode = *ppHead;
SList* pNewnode = BuyNewnode(data);
pNewnode->pNext = *ppHead;
*ppHead = pNewnode;
}
//尾删
void SListPopBack(SList* *ppHead)
{
SList* pNode = *ppHead;
SList* pOldNext = NULL;
//判断链表是否为空
if (*ppHead == NULL)
{
printf("List is none\n");
return;
}
while (pNode->pNext->pNext != NULL)
{
pNode = pNode->pNext;
}
pOldNext = pNode->pNext;
pNode->pNext = NULL;
free(pOldNext);
pOldNext = NULL;
}
//头删
void SListPopFront(SList* *ppHead)
{
//判断链表是否为空
if (*ppHead == NULL)
{
printf("List is none\n");
return;
}
SList* pNode = *ppHead;
*ppHead = pNode->pNext;
free(pNode);
pNode = NULL;
}
//在pos前插入数据
void SListInsert(SList* *ppHead, SList* pPos, Datatype data)
{
SList* pNode = *ppHead;
SList* pNewNode = BuyNewnode(data);
//特殊情况下,如果是空链表
if (*ppHead == NULL && pPos == NULL)
{
*ppHead = pNewNode;
return;
}
//在头的位置插相当于头插
if (*ppHead == pPos)
{
SListPushFront(ppHead, data);
}
else
{
while (pNode->pNext != pPos)
pNode = pNode->pNext;
pNewNode->pNext = pPos;
pNode->pNext = pNewNode;
}
}
//删除pPos位置的值
void SListErase(SList* *ppHead, SList* pPos)
{
SList* pNode = *ppHead;
if (pPos == NULL)
{
printf("指定位置没有数据\n");
return;
}
if (*ppHead == pPos)
{
SListPopFront(ppHead);
return;
}
while (pNode->pNext != pPos)
pNode = pNode->pNext;
pNode->pNext = pPos->pNext;
free(pPos);
pPos = NULL;
}
// 按值删除,只删遇到的第一个
void SListRemove(SList* *ppHead, Datatype data)
{
SList* pPos = SListFind(ppHead, 1);
//在头部删除,相当于头删
if (*ppHead == pPos)
SListPopFront(ppHead);
//其余情况下
if (pPos != NULL)
{
SListErase(ppHead, pPos);
}
}
// 按值删除,删除所有的
void SListRemoveAll(SList* *ppHead, Datatype data)
{
SList* pNode = *ppHead;
SList* pDel = NULL;
while (pNode->pNext != NULL)
{
if (pNode->pNext->data == data)
{
pDel = pNode->pNext;
pNode->pNext = pDel->pNext;
free(pDel);
pDel = NULL;
}
else
pNode = pNode->pNext;
}
if ((*ppHead)->data == data)//优先级高低
{
SListPushFront(ppHead, data);
}
}
//判断链表是否为空
int SListEmpty(SList* *ppHead)
{
if (*ppHead == NULL)
return 0;
else
return 1;
}
SList* SListFind(SList* *ppHead, Datatype data)
{
SList* pNode = *ppHead;
while (pNode != NULL)
{
if (pNode->data == data)
{
return pNode;
}
pNode = pNode->pNext;
}
return NULL;
}
Datatype SListCount(SList* *ppHead)
{
int count = 0;
SList* pNode = *ppHead;
while (pNode != NULL)
{
count++;
pNode = pNode->pNext;
}
return count;
}
最后是测试函数:
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"SList.h"
int main()
{
SList* pHead = NULL;
SList* ret = NULL;
SList* pPos = NULL;
SList* newHead = NULL;
SListInit(pHead);
SListPushBack(&pHead, 4);
SListPushBack(&pHead, 3);
SListPushBack(&pHead, 2);
SListPushBack(&pHead, 1);
SListPushFront(&pHead, 1);
SListPushFront(&pHead, 1);
SListPushFront(&pHead, 2);
SListPushFront(&pHead, 1);
SListPushFront(&pHead, 3);
SListPrint(pHead);
SListPushFront(&pHead, 1);
SListPushFront(&pHead, 6);
SListPopBack(&pHead);
SListPopFront(&pHead);
SListPrint(pHead);
pPos = SListFind(&pHead, 1);
SListInsert(&pHead, pPos, 5);
SListPrint(pHead);
pPos = SListFind(&pHead, 3);
SListErase(&pHead, pPos);
SListPrint(pHead);
SListRemove(&pHead, 1);
SListPrint(pHead);
SListRemoveAll(&pHead, 1);
SListPrint(pHead);
if (SListEmpty(&pHead))
printf("链表个数是:%d\n", SListCount(&pHead));
else
printf("链表为空\n");
pPos = SListFind(&pHead, 4);
InsertNoBianLi1(&pHead, pPos, 9);
SListPrint(pHead);
pPos = SListFind(&pHead, 4);
InsertNoBianLi2(&pHead, pPos, 9);
SListPrint(pHead);
pPos = SListFind(&pHead, 4);
SListPopNoBianLi(pPos);
SListPrint(pHead);
system("pause");
return 0;
}