1.
链表的概念及结构
概念:链表是⼀种物理存储结构上⾮连续、⾮顺序的存储结构,数据元素的逻辑顺序是通过链表
中的指针链接次序实现的 。
![](https://img-blog.csdnimg.cn/direct/650ffae2cc0f4a578a8f53cf68438c75.png)
与顺序表不同的是,链表⾥的每节"⻋厢"都是独⽴申请下来的空间,我们称之为“结点/节点”
节点的组成主要有两个部分:当前节点要保存的数据和保存下⼀个节点的地址(指针变量)。
1.链表的实现
Slist.c
#include"Slist.h"
//打印
void SLTPrint(SLlistNode* phead)
{
SLlistNode* pcur = phead;
while (pcur)
{
printf("%d->", pcur->data);
pcur = pcur->next;
}
printf("NULL\n");
}
SLlistNode* SLBuyNode(SLDatatype x)//创建一个新节点
{
SLlistNode* newnode = (SLlistNode*)malloc(sizeof(SLlistNode));
if (newnode==NULL)
{
perror("malloc file");
exit(1);
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
//尾插
void SLPushback(SLlistNode** pphead, SLDatatype x)
{
assert(pphead);
SLlistNode* newnode = SLBuyNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SLlistNode *ptail = *pphead;//找尾
while (ptail->next)
{
ptail = ptail->next;
}
ptail->next = newnode;
}
}
//头插
void SLPushfront(SLlistNode** pphead, SLDatatype x)
{
assert(pphead);
SLlistNode* newnode = SLBuyNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
newnode->next = *pphead;
*pphead = newnode;
}
}
//尾删
void SLPopback(SLlistNode** pphead)
{
assert(pphead && *pphead);
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLlistNode* pcur = *pphead;
SLlistNode* ptail = *pphead;
while (ptail->next)
{
pcur = ptail;
ptail = ptail->next;
}
free(ptail);
ptail = NULL;
pcur->next = NULL;
}
}
//头删
void SLPopfront(SLlistNode** pphead)
{
assert(pphead && *pphead);
SLlistNode* pcur = (*pphead)->next;
free(*pphead);
*pphead = pcur;
}
//查找
SLlistNode* SLTFind(SLlistNode* phead, SLDatatype x)
{
while (phead)
{
if (phead->data == x)
{
return phead;
}
phead = phead->next;
}
return NULL;
}
//在指定位置之前插入数据
void SLTInsert(SLlistNode** pphead, SLlistNode* pos, SLDatatype x)
{
assert(pphead && *pphead);
assert(pos);
if (*pphead == pos)
{
SLPushfront(pphead, x);
}
else
{
SLlistNode* pcur = *pphead;
while (pcur->next != pos)
{
pcur = pcur->next;
}
SLlistNode* newnode = SLBuyNode(x);
newnode->next = pos;
pcur->next = newnode;
}
}
//删除pos节点
void SLTErase(SLlistNode** pphead, SLlistNode* pos)
{
assert(pphead && *pphead);
assert(pos);
if (*pphead == pos)
{
SLPopfront(pphead);
}
else
{
SLlistNode* pcur = *pphead;
while (pcur->next != pos)
{
pcur = pcur->next;
}
pcur->next = pos->next;
free(pos);
pos = NULL;
}
}
//在指定位置之后插入数据
void SLTInsertAfter(SLlistNode* pos, SLDatatype x)
{
assert(pos);
SLlistNode* newnode = SLBuyNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
//删除pos之后的节点
void SLTEraseAfter(SLlistNode* pos)
{
assert(pos && pos->next);
SLlistNode* pcur = pos->next;
pos->next = pcur->next;
free(pcur);
pcur = NULL;
}
//销毁链表
void SListDesTroy(SLlistNode** pphead)
{
assert(*pphead && pphead);
SLlistNode* pcur = *pphead;
while (pcur)
{
SLlistNode* next = pcur->next;
free(pcur);
pcur = next;
}
*pphead = NULL;
}
Slist.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int SLDatatype;
typedef struct SLlistNode
{
SLDatatype data;
struct SLlistNode* next;
}SLlistNode;
//打印
void SLTPrint(SLlistNode* phead);
//尾插
void SLPushback(SLlistNode** pphead, SLDatatype x);
//头插
void SLPushfront(SLlistNode** pphead, SLDatatype x);
//尾删
void SLPopback(SLlistNode** pphead);
//头删
void SLPopfront(SLlistNode** pphead);
//查找
SLlistNode* SLTFind(SLlistNode* phead, SLDatatype x);
//在指定位置之前插入数据
void SLTInsert(SLlistNode** pphead, SLlistNode* pos, SLDatatype x);
//删除pos节点
void SLTErase(SLlistNode** pphead, SLlistNode* pos);
//在指定位置之后插入数据
void SLTInsertAfter(SLlistNode* pos, SLDatatype x);
//删除pos之后的节点
void SLTEraseAfter(SLlistNode* pos);
//销毁链表
void SListDesTroy(SLlistNode** pphead);
test.c
#include"Slist.h"
//void Test()
//{
// SLlistNode* a1 = (SLlistNode*)malloc(sizeof(SLlistNode));
// a1->data = 1;
// SLlistNode* a2 = (SLlistNode*)malloc(sizeof(SLlistNode));
// a2->data = 2;
// SLlistNode* a3 = (SLlistNode*)malloc(sizeof(SLlistNode));
// a3->data = 3;
// SLlistNode* a4 = (SLlistNode*)malloc(sizeof(SLlistNode));
// a4->data = 4;
//
// a1->next = a2;
// a2->next = a3;
// a3->next = a4;
// a4->next = NULL;
// SLTPrint(a1);
//}
int main()
{
//Test();
SLlistNode* Slist=NULL;
SLPushback(&Slist, 1);
SLPushback(&Slist, 2);
SLPushback(&Slist, 3);
SLPushback(&Slist, 4);
SLTPrint(Slist);
//SLPushfront(&Slist, 1);
//SLTPrint(Slist);
//SLPushfront(&Slist, 2);
//SLTPrint(Slist);
//SLPushfront(&Slist, 3);
//SLTPrint(Slist);
//SLPushfront(&Slist, 4);
//SLTPrint(Slist);
//尾删
//SLPopback(&Slist);
//SLTPrint(Slist);
//SLPopback(&Slist);
//SLTPrint(Slist);
//SLPopback(&Slist);
//SLTPrint(Slist);
//SLPopback(&Slist);
//SLTPrint(Slist);
//头删
/*SLPopfront(&Slist);
SLTPrint(Slist);
SLPopfront(&Slist);
SLTPrint(Slist);
SLPopfront(&Slist);
SLTPrint(Slist);
SLPopfront(&Slist);
SLTPrint(Slist);*/
SLlistNode* find = SLTFind(Slist, 3);
//if (find != NULL)
//{
// printf("找到啦");
//}
//else
//{
// printf("没有找到");
//}
/*SLTInsert(&Slist, find, 88);
SLTPrint(Slist);*/
//SLTErase(&Slist, find);
//SLTPrint(Slist);
//SLTInsertAfter(find, 11);
SLTEraseAfter(find);
SLTPrint(Slist);
SListDesTroy(&Slist);
return 0;
}