单链表的增删查改

单链表的增删查改

//无头+单向+不循环链表
#include"Slist.h"


//申请新节点
SListNode* BuySListNode(SLTDateType x)

{

SListNode* node = (SListNode*)malloc(sizeof(SListNode));

node->data = x;

node->next = NULL;



return node;

}


//打印
void SListPrint(SListNode* plist)

{

assert(plist);

SListNode* cur = plist;

while (cur)

{

printf("%d->", cur->data);

cur = cur->next;

}

printf("NULL\n");

}


//单链表的尾插
void SListPushBack(SListNode** pplist, SLTDateType x)

{

SListNode* newnode = BuySListNode(x);



if (*pplist == NULL)

{

*pplist = newnode;

}

else

{

SListNode* tail = *pplist;

while (tail->next != NULL)

{

tail = tail->next;

}

tail->next = newnode;

}

}


//单链表头插
void SListPushFront(SListNode** pplist, SLTDateType x)

{

assert(pplist);

SListNode* newnode = BuySListNode(x);

if (*pplist == NULL)

{

*pplist = newnode;

}

else

{

newnode->next = *pplist;

*pplist = newnode;

}

}


//单链表尾删
void SListPopBack(SListNode** pplist)

{

//空或者1个

//非空

SListNode* prev = NULL;

SListNode* tail = *pplist;

if (tail == NULL || tail->next == NULL)

{

free(tail);

*pplist = NULL;

}

else

{

while (tail->next != NULL)

{

prev = tail;

tail = tail->next;

}

free(tail);

prev->next = NULL;

}

}


//单链表头删
void SListPopFront(SListNode** pplist)
//空  只有一个
//非空情况
{

SListNode* first = *pplist;

if (first == NULL)

{

return;

}

if (first->next == NULL)

{

free(first);

*pplist = NULL;

}

else

{

SListNode* next = first->next;

free(first);

*pplist = next;

}

}


//查找
SListNode* SListFind(SListNode* plist, SLTDateType x)

{

assert(plist);

SListNode* cur = plist;

while (cur)

{

if (cur->data == x)

return cur;

else

cur = cur->next;

}

return NULL;

}


//在任意位置后插入
void SListInsertAfter(SListNode* pos, SLTDateType x)

{

assert(pos);



SListNode* newnode = BuySListNode(x);

SListNode* next = pos->next;

pos->next = newnode;

newnode->next = next;

}


//在任意位置后删除
void SListEraseAfter(SListNode* pos)

{

assert(pos);

SListNode* next = pos->next;

if (next != NULL)

{

SListNode* nextnext = next->next;

free(next);

pos->next = nextnext;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值