链表在物理储存结构上是非连续的,它主要是通过指针指向下一个数据的。
节点都是动态开辟出来的。
我们把数据和指向下一个节点地址的指针叫做一个节点。
注:本代码的实现的是动态的
ctypedef int SLTypeDate;
typedef struct SListNode
{
SLTypeDate date;
struct SListNode* next;
}SListNode;
cSListNode* BuySListNode(SLTypeDate x)
{
SListNode* NewNode = (SListNode*)malloc(sizeof(SListNode));
if (NewNode == NULL)
exit(-1);
NewNode->date = x;
NewNode->next = NULL;
return NewNode;
}
cvoid SListPrint(SListNode* phead)
{
while (phead)
{
printf("%d->", phead->date);
phead = phead->next;
}
printf("NULL\n");
}
从头节点一个一个进行销毁
cvoid SListDestory(SListNode** pphead)
{
assert(pphead);
SListNode* cur =*pphead;
while (cur)
{
cur = (*pphead)->next;
free(*pphead);
*pphead = cur;
}
}
cvoid SListPushFront(SListNode** pphead, SLTypeDate x)
{
assert(pphead);
SListNode* NewNode=BuySListNode(x);
SListNode* NewNode = (SListNode*)malloc(sizeof(SListNode));
if (NewNode == NULL)
exit(-1);
NewNode->date = x;
NewNode->next = *pphead;
*pphead = NewNode;
}
尾插的时候也要注意没有节点的情况。
cvoid SListPushBack(SListNode** pphead, SLTypeDate x)
{
assert(pphead);
SListNode* NewNode = BuySListNode(x);
if (*pphead == NULL)
{
*pphead = NewNode;
}
else
{
SListNode* tail = *pphead;
while (tail->next)
{
tail = tail->next;
}
tail->next = NewNode;
}
}
需要判断节点为1,还有多个的情况
cvoid SListPopBack(SListNode** pphead)
{
assert(*pphead);
if ((*pphead)->next==NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SListNode* tail = *pphead;
while (tail->next->next != NULL)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}
先用临时变量储存起来头节点后面的节点
cvoid SListPopFront(SListNode** pphead)
{
assert(*pphead);
SListNode* cur = (*pphead)->next;
free(*pphead);
*pphead = cur;
}
查找到返回改节点的地址,查找不到返回空指针
cSListNode* SListFind(SListNode* phead, SLTypeDate x)
{
assert(phead);
while (phead->date!=x)
{
phead = phead->next;
if (phead == NULL)
return NULL;
}
return phead;
}
1.pos不能为空指针 2.注意在第一个节点前插的情况
cvoid SListInsertPre(SListNode** pphead, SListNode* pos, SLTypeDate x)
{
assert(pphead);
assert(pos);
SListNode* NewNode = BuySListNode(x);
if (pos==*pphead)
{
//NewNode->next = *pphead;
//*pphead = NewNode;
SListPushFront(pphead,x);
}
else
{
SListNode* cur = *pphead;
while (cur->next != pos)
{
cur = cur->next;
}
NewNode->next = pos;
cur->next = NewNode;
}
}
cvoid SListInsertAfter(SListNode* pos, SLTypeDate x)
{
assert(pos);
SListNode* NewNode = BuySListNode(x);
NewNode->next = pos->next;
pos->next = NewNode;
}
cvoid SListErase(SListNode** pphead, SListNode* pos)
{
assert(pphead);
assert(pos);
if (pos == *pphead)
{
//*pphead = (*pphead)->next;
//free(pos);
//pos = NULL;
SListPopFront(pphead);
}
else
{
SListNode* cur = *pphead;
while (cur->next != pos)
{
cur = cur->next;
}
cur->next = pos->next;
free(pos);
pos = NULL;
}
}
注意为最后一个节点的时候,不能进行删除
cvoid SListEraseAfter(SListNode* pos)
{
assert(pos);
if (pos->next == NULL)
{
return;
}
SListNode* cur = pos->next->next;
free(pos->next);
pos->next = cur;
}