1 Star 1 Fork 0

LC.yulin/数据结构

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test_9_22.c 2.35 KB
一键复制 编辑 原始数据 按行查看 历史
LC.yulin 提交于 2022-09-22 15:14 . C语言实现链表
#include"SList_9_22.h"
void SListPrint(SL* phead)
{
SL* cur = phead;
while (cur)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}
SL* BuySLNode(SL** pphead, SLDataType x)
{
assert(pphead);
SL* newnode = (SL*)malloc(sizeof(SL));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
newnode->next = NULL;
newnode->data = x;
return newnode;
}
void SListPushBack(SL** pphead, SLDataType x)
{
assert(pphead);
SL* newnode = BuySLNode(pphead, x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
SL* tail = *pphead;
while (tail->next !=NULL)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SListPopBack(SL** pphead)
{
assert(pphead);
assert(*pphead);
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SL* prev = NULL;
SL* cur = *pphead;
while (cur->next != NULL)
{
prev = cur;
cur = cur->next;
}
prev->next = NULL;
free(cur);
}
}
void SListPushFront(SL** pphead, SLDataType x)
{
assert(pphead);
SL* newnode = BuySLNode(pphead, x);
newnode->next = *pphead;
*pphead = newnode;
}
void SListPopFront(SL** pphead)
{
assert(pphead);
assert(*pphead);
SL* prev = (*pphead)->next;
free(*pphead);
*pphead = prev;
}
SL* SListFind(SL* phead, SLDataType x)
{
SL* cur = phead;
while (cur->next != NULL)
{
if (cur->data == x)
{
return cur;
}
cur = cur->next;
}
return NULL;
}
void SListInsert(SL** pphead, SL* pos, SLDataType x)
{
assert(pphead);
assert(pos);
SL* newnode = BuySLNode(pphead, x);
if (*pphead == pos)
{
newnode->next = *pphead;
*pphead = newnode;
}
else
{
SL* cur = *pphead;
while (cur->next != pos)
{
cur = cur->next;
}
newnode->next = cur->next;
cur->next = newnode;
}
}
void SListInsertAfter(SL** pphead, SL* pos, SLDataType x)
{
assert(pphead);
assert(pos);
SL* newnode = BuySLNode(pphead, x);
newnode->next = pos->next;
pos->next = newnode;
}
void SListEarse(SL** pphead, SL* pos)
{
assert(pphead);
assert(pos);
if (*pphead == pos)
{
SL* cur = (*pphead)->next;
free(*pphead);
*pphead = cur;
}
else
{
SL* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
}
}
void SListDestory(SL** pphead)
{
assert(pphead);
SL* prev = *pphead;
while (prev->next != NULL)
{
SL* cur = prev->next;
free(prev);
prev = cur;
}
*pphead = NULL;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/lc-yulin/data-structure.git
git@gitee.com:lc-yulin/data-structure.git
lc-yulin
data-structure
数据结构
master

搜索帮助