链表: 节点 包含一个指针跟一个数据存储,存放了下一个节点的地址(单向链表),通过上一个节点的指针域去找下一个数据。
代码实现链表的操作函数:初始化;尾部插入 ;头部插入 ;给定结点插入插入到结点前 ;尾部删除 ;头部删除;给定结点删除; 按值删除只删遇到的第一个;
按值删除删除所有的;按值查找,返回第一个找到的结点指针,如果没找到,返回 NULL ; 销毁 ;
sqList.h中代码:
#pragma once
typedef int DataType;
typedef struct SlistNode
{
DataType data;
struct SlistNode *pNext;
}Slist;
//初始化
void SlistInit(Slist *pFirst);
// 尾部插入
void SlistPushBack(Slist* pFirst, DataType data);
// 头部插入
void SlistPushFront(Slist *pFirst, DataType data);
//给定结点插入,插入到结点前
void SlistInsert(Slist *pFirst, Slist *pPos, DataType data);
// 头部删除
void SlistPopFront(Slist *pFirst);
//尾部删除
void SlistpopBack(Slist *Fister);
//按值删除
void SeqListRemove(Slist *pSL, DataType data);
//按值删除,删除所有的
void SeqListRemoveAll(Slist *pSL, DataType data);
//给定结点删除
void SlistErase(Slist *pFirst, Slist *pPos);
//销毁
void SlistDestroy(Slist *pFirst);
// 按值查找,返回第一个找到的结点指针,如果没找到,返回 NULL
Slist *SeqListFind(Slist *pFirst, DataType data);
// 返回数量
int SeqListSize(Slist *pSL);
//显示
void SlistShow(Slist *pFirst);
main.c中代码:
#include<stdio.h>
#include<stdlib.h>
#include"Sqlist.h"
#include<assert.h>
//初始化
void SlistInit(Slist *pFirst)
{
pFirst->pNext = NULL;
}
//创建一块新空间
Slist* SlistBuyNewNode(DataType data)
{
Slist *pNewNode = (Slist*)calloc(sizeof(Slist), 1);
if (pNewNode!=NULL)
{
pNewNode->data = data;
pNewNode->pNext = NULL;
}
return pNewNode;
}
//尾插
void SlistPushBack(Slist *pFirst,DataType data)
{
Slist *pNewNode=SlistBuyNewNode(data);
Slist *pNode = pFirst;
while (pNode->pNext != NULL)
{
pNode = pNode->pNext;
}
pNewNode->pNext = pNode->pNext;
pNode->pNext = pNewNode;
}
// 头部插入
void SlistPushFront(Slist *pFirst, DataType data)
{
Slist *pNewNode = SlistBuyNewNode(data);
Slist *pNode = pFirst;
pNewNode->pNext = pNode->pNext;
pNode ->pNext= pNewNode;
}
//给定结点插入,插入到结点前
void SlistInsert(Slist *pFirst, Slist *pPos, DataType data)
{
if (pFirst == NULL&&pPos == NULL)
{
pFirst= SlistBuyNewNode(data);
}
if (pFirst == pPos)
{
SlistPushFront(pFirst, data);
return;
}
Slist *pNode = pFirst;
while (pNode->pNext != pPos)
{
pNode = pNode->pNext;
}
Slist *pNewNode = SlistBuyNewNode(data);
pNewNode->pNext = pPos;
pNode->pNext = pNewNode;
}
// 头部删除
void SlistPopFront(Slist *pFirst)
{
Slist *pHead = pFirst;
Slist *pNext = pHead->pNext;
pFirst->pNext = pNext->pNext;
free(pNext);
}
//尾部删除
void SlistpopBack(Slist *pFirst)
{
Slist *pNode = pFirst;
Slist *pNext = pNode->pNext;
while (pNode->pNext->pNext != NULL)
{
pNode = pNode->pNext;
}
pNext = pNode->pNext;
pNode->pNext=pNext->pNext;
free(pNext);
}
//找值
Slist *SeqListFind(Slist *pFirst, DataType data)
{
assert(pFirst != NULL);
Slist *pHead=pFirst;
while (pHead!= NULL)
{
if (pHead->data == data)
{
return pHead;
}
pHead = pHead->pNext;
}
return NULL;
}
//显示
void SlistShow(Slist *pFirst)
{
Slist *pNode=pFirst;
while (pNode->pNext != NULL)
{
pNode = pNode->pNext;
printf("%d->", pNode->data);
}
printf("NULL\n");
}
// 返回数量
int SeqListSize(Slist *pFirst)
{
assert(pFirst != NULL);
Slist *pNode = pFirst;
int count = 0;
while (pNode->pNext != NULL)
{
pNode = pNode->pNext;
count++;
}
return count;
}
//按值删除
void SeqListRemove(Slist *pFirst, DataType data)
{
assert(pFirst != NULL);
Slist *ret=SeqListFind(pFirst, data);
if (NULL != ret)
{
SlistErase(pFirst, ret);
}
else
{
printf("没有这个数\n");
}
}
//按值删除,删除所有的
void SeqListRemoveAll(Slist *pFirst, DataType data)
{
assert(pFirst != NULL);
Slist *pNode = pFirst;
while (pNode->pNext!= NULL)
{
Slist *ret = SeqListFind(pFirst, data);
if (ret != NULL) {
SeqListRemove(pFirst, data);
}
pNode = pNode->pNext;
}
}
//给定结点删除
void SlistErase(Slist *pFirst, Slist *pPos)
{
assert(pFirst != NULL);
assert(pPos != NULL);
if (pFirst == pPos)
{
SlistPopFront(pPos);
}
else
{
Slist *pHead = pFirst;
while (pHead->pNext != pPos)
{
pHead = pHead->pNext;
}
pHead->pNext = pHead->pNext->pNext;
free(pPos);
}
}
//销毁
void SlistDestroy(Slist *pFirst)
{
assert(pFirst != NULL);
Slist *pHead = pFirst->pNext;
Slist *pNext;
while (pHead != NULL)
{
pNext= pHead->pNext;
free(pHead);
pHead = pNext;
}
pFirst->pNext = NULL;
printf("销毁成功\n");
}
int main()
{
Slist pHead;
SlistInit(&pHead);
printf("尾插: ");
SlistPushBack(&pHead, 1);
SlistPushBack(&pHead, 2);
SlistPushBack(&pHead, 3);
SlistPushBack(&pHead, 4);
SlistShow(&pHead);
printf("头插: ");
SlistPushFront(&pHead, 1);
SlistPushFront(&pHead, 2);
SlistPushFront(&pHead, 3);
SlistPushFront(&pHead, 4);
SlistShow(&pHead);
printf("头删: ");
SlistPopFront(&pHead);
SlistPopFront(&pHead);
SlistShow(&pHead);
printf("尾删: ");
SlistpopBack(&pHead);
SlistpopBack(&pHead);
SlistShow(&pHead);
printf("删除4: ");
SlistPushBack(&pHead, 4);
SeqListRemove(&pHead, 1);
SlistShow(&pHead);
printf("删除所有的2 : ");
SeqListRemoveAll(&pHead, 2);
SlistShow(&pHead);
printf("查找4 : ");
Slist* ret=SeqListFind(&pHead, 4);
if (NULL!= ret)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
int num = SeqListSize(&pHead);
printf("个数为%d:\n", num);
SlistDestroy(&pHead);
SlistShow(&pHead);
system("pause");
return 0;
}