代码拉取完成,页面将自动刷新
#include "SList.h"
//链表的初始化以及删除
void SLInit(SLTNode** pphead)
{
assert(pphead);
*pphead = NULL;
}
void SLDestroy(SLTNode** pphead)
{
assert(pphead);
SLTNode* pure = *pphead;
SLTNode* next = (*pphead)->next;
while (pure != NULL)
{
free(pure);
pure = next;
if (next == NULL)//注意netx指针为空的情况
next = NULL;
else
next = next->next;
}
}
//创建新节点
SLTNode* NewNode(SLTDataType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
newnode->data = x;
newnode->next = NULL;
return newnode;
}
//打印链表
void SLPrintf(SLTNode* phead)
{
assert(phead);//链表不为空进行打印
SLTNode* ptail = phead;
while (ptail != NULL)
{
printf("%d->", ptail->data);
ptail = ptail->next;
}
printf("NULL\n");
}
//具体位置
SLTNode* SLTNSituation(SLTNode* phead,SLTDataType x)
{
assert(phead);
SLTNode* pcur = phead;
while (pcur->data != x)
pcur = pcur->next;
return pcur;
}
//链表的插入
//头插
void SLPushFront(SLTNode** pphead, SLTDataType x)
{
assert(pphead);
SLTNode* phead = NewNode(x);
//链表为空
if (*pphead == NULL)
{
*pphead = phead;
}
//链表不为空
phead->next = *pphead;
*pphead = phead;
}
//尾插
void SLPushBack(SLTNode** pphead, SLTDataType x)
{
SLTNode* pure = NewNode(x);
SLTNode* ptail = *pphead;
//链表为空,头节点要进行改变
if (*pphead == NULL)
{
*pphead = pure;
return;
}
//链表不为空
//找到尾结点进行插入
while (ptail->next != NULL)
ptail = ptail->next;
ptail->next = pure;
}
//任意位置插入
//前面
void SLPush1(SLTNode* phead, SLTNode* pure, SLTDataType x)
{
assert(phead);
SLTNode* p = phead;
SLTNode* pcur = NewNode(x);
while (p->next != pure)
p = p->next;
p->next = pcur;
pcur->next = pure;
}
//后面
void SLPush2(SLTNode* phead, SLTNode* pure, SLTDataType x)
{
assert(phead);
SLTNode* pcur = NewNode(x);
SLTNode* next = pure->next;
pure->next = pcur;
pcur->next = next;
}
//头删
void SListFrontDes(SLTNode** pphead)
{
assert(pphead);
assert(*pphead);
SLTNode* next = (*pphead)->next;
free(*pphead);
(*pphead) = next;//改变头结点的位置
}
//尾删
void SListBackDes(SLTNode* phead)
{
assert(phead);
SLTNode* ptail = phead;
SLTNode* pcur = phead->next;
while (pcur->next != NULL)//找到尾结点,将其进行删除
{
pcur = pcur->next;
ptail = ptail->next;
}
free(pcur);
ptail->next = NULL;
}
//任意位置删除
void SListDes(SLTNode* phead, SLTNode* p)
{
assert(phead);
SLTNode* pure = phead;
while (pure->next != p)
{
pure = pure->next;
}
pure->next = p->next;
free(p);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。