单链表接口函数的实现(增删查改)

一、单链表的实现形式以及接口函数的声明

复制代码
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
typedef int DataType ;

typedef struct SListNode
{
	DataType data;
	struct SListNode* next;
}SLTNODE;
void SLTPrint(SLTNODE* phead);//打印链表
SLTNODE* SLTBuyNode(DataType x);//创建节点
void SLTPushBack(SLTNODE** phead, DataType x);//尾插
void SLTPushFront(SLTNODE** phead, DataType x);//头插
void SLTPopBack(SLTNODE** phead);//尾删
void SLTPopFront(SLTNODE** phead);//头删
SLTNODE* SLTFind(SLTNODE* phead, DataType x);//查找
void SLTInsert(SLTNODE** phead, SLTNODE* pos, DataType x);//待定位置之前的插入
void SLTInsertAfter( SLTNODE* pos, DataType x);//待定位置之后的插入
void SLTErase(SLTNODE** phead, SLTNODE* pos);//删除待定节点
void SLTEraseAfter( SLTNODE* pos);//删除待定节点之后的节点
void SLTDestroy(SLTNODE** phead);//销毁链表

二、接口函数的实现

1.打印链表

代码如下(示例):

c 复制代码
void SLTPrint(SLTNODE* phead)//打印链表
{
    SLTNODE* cur = phead;
    while (cur)
    {

        printf("%d->", cur->data);
        cur = cur->next;
    }
    puts("NULL");
}

2.创建节点

代码如下(示例):

c 复制代码
SLTNODE* SLTBuyNode(DataType x)//创建节点
{
    SLTNODE* newnode = (SLTNODE*)malloc(sizeof(SLTNODE));
    assert(newnode);
    newnode->data = x;
    newnode->next = NULL;
    return newnode;
}

3.尾插

代码如下(示例):

void SLTPushBack(SLTNODE** phead, DataType x)//尾插

{

assert(phead);

SLTNODE* newnode = SLTBuyNode(x);

if (*phead == NULL)

{

*phead = newnode;

}

else

{

SLTNODE* tail = *phead;

while (tail->next)

{

tail = tail->next;

}

tail->next = newnode;

}

}

4.头插

代码示例:

void SLTPushFront(SLTNODE** phead, DataType x)//头插

{

assert(phead);

SLTNODE* newnode = SLTBuyNode(x);

newnode->next = *phead;

*phead = newnode;

}

5.尾删

代码示例:

void SLTPopBack(SLTNODE** phead)//尾删

{

assert(phead && *phead);

if ((*phead)->next == NULL)

{

free(*phead);

*phead = NULL;

}

else

{

SLTNODE* prev = NULL;

SLTNODE* tail = *phead;

while (tail->next)

{

prev = tail;

tail = tail->next;

}

free(tail);

prev->next = NULL;

}

}

6.头删

代码示例:

void SLTPopFront(SLTNODE** phead)//头删

{

assert(phead && *phead);

SLTNODE* next = (*phead)->next;

free(*phead);

*phead = next;

}

7.查找

代码示例:

SLTNODE* SLTFind(SLTNODE* phead, DataType x)//查找

{

assert(phead);

SLTNODE* cur = phead;

while (cur)

{

if (cur->data == x)

{

return cur;

}

cur = cur->next;

}

return NULL;

}

8.指定位置之前的插入

代码如下:

void SLTInsert(SLTNODE** phead, SLTNODE* pos, DataType x)//待定位置之前的插入

{

assert(phead && *phead);

assert(pos);

if (pos == *phead)

{

SLTPushFront(phead, x);//头插

}

else

{

SLTNODE* newnode = SLTBuyNode(x);

SLTNODE* prev = *phead;

while (prev->next!=pos)

{

prev = prev->next;

}

newnode->next = prev->next;

prev -> next = newnode;

}

}

9.指定位置之后的插入

void SLTInsertAfter( SLTNODE* pos, DataType x)//待定位置之后的插入

{

assert(pos);

SLTNODE* newnode = SLTBuyNode(x);

newnode->next = pos->next;

pos->next = newnode;

}

10.删除待定位置的节点

代码示例:

void SLTErase(SLTNODE** phead, SLTNODE* pos)//删除待定节点

{

assert(phead && *phead);

assert(pos);

if (pos == *phead)

{

SLTPopFront(phead);

}

else

{

SLTNODE* prev = *phead;

while (prev->next != pos)

{

prev = prev->next;

}

prev->next = pos->next;

free(pos);

}

}

11.销毁链表

代码示例:

void SLTDestroy(SLTNODE** phead)//销毁链表

{

assert(*phead && phead);

SLTNODE* cur = *phead;

while (cur)

{

SLTNODE* next = cur->next;

free(cur);

cur = next;

}

*phead = NULL;

}


相关推荐
Humbunklung2 小时前
Rust 控制流
开发语言·算法·rust
鑫鑫向栄3 小时前
[蓝桥杯]取球博弈
数据结构·c++·算法·职场和发展·蓝桥杯·动态规划
m0_634448894 小时前
从上下文学习和微调看语言模型的泛化:一项对照研究
学习·算法·语言模型
Once_day4 小时前
代码训练LeetCode(21)跳跃游戏2
算法·leetcode
德先生&赛先生5 小时前
LeetCode-934. 最短的桥
算法·leetcode·职场和发展
CCF_NOI.6 小时前
洛谷P12610 ——[CCC 2025 Junior] Donut Shop
算法
鑫鑫向栄7 小时前
[蓝桥杯]模型染色
数据结构·c++·算法·职场和发展·蓝桥杯
1白天的黑夜19 小时前
栈-20.有效的括号-力扣(LeetCode)
c++·算法·leetcode