1 Star 0 Fork 0

Admin / text_cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SList.c 2.42 KB
一键复制 编辑 原始数据 按行查看 历史
Admin 提交于 2024-05-08 20:00 . 单链表
#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);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/small-c_2_0/text_cpp.git
git@gitee.com:small-c_2_0/text_cpp.git
small-c_2_0
text_cpp
text_cpp
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891