概念:链表是⼀种物理存储结构上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。
链表的结构跟火车车厢相似,淡季时车次的车厢会相应减少,旺季时车次的车厢会额外增加几节。只需要将火车里的某节车厢去掉/加上,不会影响其他车厢,每节车厢都是独立存在的。
与顺序表不同的是,链表里的每节"车厢"都是独立申请下来的空间,我们称之为“结点/节点”。节点的组成主要有两个部分:当前节点要保存的数据和保存下⼀个节点的地址(指针变量)
。
为什么还需要指针变量来保存下⼀个节点的位置?
链表中每个节点都是独立申请的(即需要插入数据时才去申请⼀块节点的空间),我们需要通过指针变量来保存下⼀个节点位置才能从当前节点找到下⼀个节点。
因此链表的结构我们可以这样设计:
typedef int SLTDataType;
typedef struct SListNode
{
SLTDataType data; //节点数据
struct SListNode* next; //指针保存下⼀个节点的地址
}SLTNode;
注意:
//创建节点
SNode* BuyNode(SLDatatype x)
{
SNode* newnode = (SNode*)malloc(sizeof(SNode));
if (!newnode)
{
perror("malloc");
return NULL;
}
newnode->data = x;
newnode->next = NULL;
return newnode;
}
//尾插
void SListPushBack(SNode** pphead, SLDatatype x)
{
assert(pphead);
SNode* newnode = BuyNode(x);
//链表为空
if (*pphead == NULL)
{
*pphead = newnode;
return;
}
SNode* tail = *pphead;
//找尾
while (tail->next)
{
tail = tail->next;
}
//连接
tail->next = newnode;
}
//打印
void SListPrint(SNode* phead)
{
SNode* cur = phead;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
//头插
void SlistPushFront(SNode** pphead, SLDatatype x)
{
assert(pphead);
SNode* newnode = BuyNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SlistPopBack(SNode** pphead)
{
assert(pphead);
//链表不为空
assert(*pphead);
//只有一个节点
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
return;
}
//多个节点
SNode* cur = *pphead;
SNode* prev = NULL;
while(cur->next)
{
prev = cur;
cur = cur->next;
}
//释放尾节点
free(cur);
cur = NULL;
prev->next = NULL;
}
void SlistPopFront(SNode** pphead)
{
assert(pphead);
assert(*pphead);
SNode* cur = *pphead;
*pphead = (*pphead)->next;
free(cur);
cur = NULL;
}
找到目标,返回指向该目标的指针
//查找
SNode* SListFind(SNode** pphead, SLDatatype x)
{
assert(pphead);
SNode* cur = *pphead;
while (cur)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
//指定位置后插入
void SListInsertAfter(SNode* pos, SLDatatype x)
{
assert(pos);
SNode* newnode = BuyNode(x);
newnode->next = pos->next;
pos->next = newnode;
}
void SListInsertBefore(SNode** pphead, SNode* pos, SLDatatype x)
{
assert(pphead);
assert(pos);
//指定位置为头节点,执行头插
if (pos == *pphead)
{
SlistPushFront(pphead, x);
return;
}
//找pos位置的前驱
SNode* cur = *pphead;
while (cur->next != pos)
{
cur = cur->next;
}
SNode* newnode = BuyNode(x);
cur->next = newnode;
newnode->next = pos;
}
void SListDelePos(SNode** pphead, SNode* pos)
{
assert(pphead);
assert(pos);
//指定位置为头节点,执行头删
if (pos == *pphead)
{
SlistPopFront(pphead);
return;
}
SNode* cur = *pphead;
while (cur->next != pos)
{
cur = cur->next;
}
cur->next = pos->next;
free(pos);
pos = NULL;
}
void SListDeleAfter(SNode* pos)
{
assert(pos);
//后面得有元素
assert(pos->next);
SNode* dele = pos->next;
pos->next = dele->next;
free(dele);
dele = NULL;
}
void SListDestroy(SNode** pphead)
{
assert(pphead);
assert(*pphead);
SNode* next = NULL;
while (*pphead)
{
next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
}
链表的结构非常多样,以下情况组合起来就有8种(2x2x2)链表结构:
虽然有这么多的链表的结构,但是我们实际中最常用还是两种结构: 单链表和双向带头循环链表