代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。