SList.h(函数声明)
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLTDataType;
typedef struct SListNode
{
SLTDataType data;
struct SListNode* next;
}SLTNode;//Single List Node
//打印链表
void SListPrint(SLTNode* phead);
//尾插
void SListPushBack(SLTNode** pphead, SLTDataType x);
//头插
void SListPushFront(SLTNode** pphead, SLTDataType x);
//头删
void SListPopFront(SLTNode** pphead);
//尾删
void SListPopBack(SLTNode** pphead);
//查找结点
SLTNode* SListFind(SLTNode* phead, SLTDataType x);
//在地址pos之前插入结点
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
//删除pos位置的结点
void SListErase(SLTNode** pphead, SLTNode* pos);
//在pos之后插入新结点
void SListInsertAfter(SLTNode* pos, SLTDataType x);
//删除pos之后的结点
void SListEraseAfter(SLTNode* pos);
//链表的销毁
void SListDestroy(SLTNode** pphead);
SList.c(函数实现)
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
//动态开辟一个新结点
SLTNode* BuySListNode(SLTDataType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
assert(newnode);
newnode->data = x;
newnode->next = NULL;
return newnode;
}
//打印链表
void SListPrint(SLTNode* phead)
{
SLTNode* cur = phead;//这里用cur标识结点地址,增强可读性。
while (cur != NULL)//cur不为NULL,说明cur有具体指向的结构体内容。
{
printf("%d->", cur->data);
cur=cur->next;
}
printf("NULL\n");
}
//尾插
void SListPushBack(SLTNode** pphead, SLTDataType x)
{
assert(pphead);
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
assert(newnode);
newnode->data = x;
newnode->next = NULL;//SLTNode* newnode=BuySListNode(x);代替新结点的创建
if (*pphead == NULL)//二级指针变量pphead存储的是plist的地址,*pphead就是plist,这里判断plist是否为NULL。
{
*pphead = newnode;
}
else
{
SLTNode* tail = *pphead;//*pphead==plist指针变量(plist存储着头结点的地址)。tail标识符是为了增加代码可读性。
while (tail->next != NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
//本质:修改函数之外的变量,必须利用变量的地址来修改。
//1、链表中所有的结点都是用指针变量来表示,指针变量可以存储结点的地址。每一个结构体数据存储在内存中都是通过指针来访问,不通过变量名来访问。
//2、当链表的头结点为非NULL时,就可以通过next遍历所有结点,从而改变链表上的任意结点的数据。当链表的头结点为NULL时,即plist指向的都不是结构体的地址,而是NULL,那么我们需要改变plist的值,让其指向结构体(新结点),而改变一级指针就需要传二级指针。
}
//头插
void SListPushFront(SLTNode** pphead, SLTDataType x)
{
assert(pphead);
SLTNode* newnode = BuySListNode(x);//BuySListNode函数创建的新结点的next指向NULL。
newnode->next = *pphead;
*pphead = newnode;//这一步改变plist的值,所以需要传二级指针。
}
//头删
void SListPopFront(SLTNode** pphead)//要改变plist就必须传plist的地址。
{
assert(pphead);
assert(*pphead);//判是否为空链表,防止解引用空指针。
SLTNode* next = (*pphead)->next;//先把下一个结点的地址保存,防止free之后找不到。
free(*pphead);
*pphead = next;
}
//尾删
void SListPopBack(SLTNode** pphead)//如果传的是首结点地址
{
assert(pphead);
assert(*pphead);
if ((*pphead)->next == NULL)//只有一个结点
{
free(*pphead);
*pphead = NULL;
}
else
{
SLTNode* tail = *pphead;
SLTNode* tailPrev = NULL;//记录被删结点的前一个结点,用于给前一个结点的next置空,不然会出现野指针。
while (tail->next != NULL)
{
tailPrev = tail;
tail = tail->next;
}
free(tail);
tail = NULL;
tailPrev->next = NULL;
}
}
//查找函数
SLTNode* SListFind(SLTNode* phead, SLTDataType x)
{
assert(phead);
SLTNode* cur = phead;
while (cur->data != x)
{
if (cur->next == NULL)
{
return NULL;
}
else
{
cur = cur->next;
}
}
return cur;
}
//在地址pos之前插入结点
//缺点:需要找到前一个结点的位置。如果是在pos后面插入时间复杂度更快。
void SListInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)//有可能为头插,所以传二级指针。
{
assert(pphead);
assert(pos);//在NULL前面插入不适合,规矩。
SLTNode* cur = *pphead;
if (cur == pos)
{
SListPushFront(pphead, x);
}
else
{
while (cur->next != pos)
{
cur = cur->next;
}
SLTNode* newnode = BuySListNode(x);
newnode->next = cur->next;
cur->next = newnode;
}
}
//删除pos位置的结点
//缺点:需要找到前一个结点的位置。如果是删除pos后面的结点时间复杂度更快。
void SListErase(SLTNode** pphead, SLTNode* pos)
{
assert(pphead);
assert(pos);//*pphead为NULL,pos不可能不为NULL.
SLTNode* cur = *pphead;
if (pos == *pphead)
{
SListPopFront(pphead);
}
else
{
while (cur->next != pos)
{
cur = cur->next;
}
cur->next = pos->next;
free(pos);
pos = NULL;
}
}
//在pos之后插入新结点
void SListInsertAfter(SLTNode* pos, SLTDataType x)
{
assert(pos);
SLTNode* newnode = BuySListNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
//删除pos之后的结点
void SListEraseAfter(SLTNode* pos)
{
assert(pos);
SLTNode* tmp = pos->next;
if (tmp == NULL)
{
return;
}
else
{
pos->next = pos->next->next;
free(tmp);
tmp = NULL;
}
}
//链表的销毁
void SListDestroy(SLTNode** pphead)
{
assert(pphead);
if (*pphead == NULL)
return;
SLTNode* next = (*pphead)->next;
free(*pphead);
*pphead = NULL;
SLTNode* cur = next;
while (cur)
{
next = next->next;
free(cur);
cur = next;
}
}
Test.c(函数测试)
#define _CRT_SECURE_NO_WARNINGS 1
#include"SList.h"
void TestSList1()//测试函数SListPrint
{
SLTNode* n1 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n1);
SLTNode* n2 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n2);
SLTNode* n3 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n3);
SLTNode* n4 = (SLTNode*)malloc(sizeof(SLTNode));
assert(n4);
n1->data = 1;
n2->data = 2;
n3->data = 3;
n4->data = 4;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = NULL;
SListPrint(n1);//需要链表第一个结点地址(这里的这些结构体都没有变量名,都是以指针的形式访问)
}
void TestSList2()//测试函数SListPushBack
{
SLTNode* plist = NULL;
SListPushBack(&plist, 1);//传一级指针的地址(二级指针)
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);
}
void TestSList3()//测试函数SListPushFront
{
SLTNode* plist = NULL;
SListPushFront(&plist, 4);
SListPushFront(&plist, 3);
SListPushFront(&plist, 2);
SListPushFront(&plist, 1);
SListPrint(plist);
}
void TestSList4()//测试函数SListPopFront
{
SLTNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);//尾插1、2、3、4,并打印。
SListPopFront(&plist);
SListPopFront(&plist);
SListPopFront(&plist);
SListPopFront(&plist);
SListPrint(plist);//头删完所有元素,并打印。
SListPopFront(&plist);//无数据可删,再删就断言。
SListPrint(plist);
}
void TestSList5()//测试函数SListPopBack
{
SLTNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);//尾插1、2、3、4,并打印。
SListPopBack(&plist);
SListPopBack(&plist);
SListPopBack(&plist);
SListPopBack(&plist);
SListPrint(plist);//尾删完所有元素,并打印。
SListPopBack(&plist);//无数据可删,再删就断言。
SListPrint(plist);
}
void TestSList6()//测试函数SListFind,并修改该结点。
{
SLTNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);
SLTNode* ret = SListFind(plist, 3);//查找结点
if (ret)
{
printf("找到结点\n");
ret->data = 30;// 修改结点的数据
SListPrint(plist);
}
else
{
printf("未找到结点\n");
}
}
void TestSList7()//测试函数SListInsert
{
SLTNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);
SLTNode* ret = SListFind(plist, 3);//寻找结点数据为3的地址
SListInsert(&plist, ret, 30);//将结点数据为3的前面插入数据为30结点
SListPrint(plist);
ret = SListFind(plist, 1);//寻找结点数据为1的地址
SListInsert(&plist, ret, 10); //将结点数据为1的地址前面插入数据为10结点
SListPrint(plist);
}
void TestSList8()//测试函数SListErase
{
SLTNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);
SLTNode* ret = SListFind(plist, 1);
SListErase(&plist, ret);
SListPrint(plist);
ret = SListFind(plist, 4);
SListErase(&plist, ret);
SListPrint(plist);
}
void TestSList9()//测试函数SListInsertAfter
{
SLTNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);
SLTNode* ret = SListFind(plist, 1);
SListInsertAfter(ret, 10);
SListPrint(plist);
ret = SListFind(plist, 4);
SListInsertAfter(ret, 40);
SListPrint(plist);
}
void TestSList10()//测试函数SListEraseAfter
{
SLTNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);
SLTNode* ret = SListFind(plist, 1);
SListEraseAfter(ret);
SListPrint(plist);
ret = SListFind(plist, 3);
SListEraseAfter(ret);
SListPrint(plist);
}
void TestSList11()//测试函数SListDestroy
{
SLTNode* plist = NULL;
SListPushBack(&plist, 1);
SListPushBack(&plist, 2);
SListPushBack(&plist, 3);
SListPushBack(&plist, 4);
SListPrint(plist);
SListDestroy(&plist);
SListPrint(plist);
}
int main()
{
/*TestSList1();
TestSList2();
TestSList3();
TestSList4();
TestSList5();
TestSList6();
TestSList7();
TestSList8();
TestSList9();
TestSList10();
TestSList11();*/
return 0;
}