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

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

复制代码
#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;

}


相关推荐
Fanxt_Ja19 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下19 小时前
最终的信号类
开发语言·c++·算法
茉莉玫瑰花茶19 小时前
算法 --- 字符串
算法
博笙困了19 小时前
AcWing学习——差分
c++·算法
NAGNIP19 小时前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP19 小时前
大模型微调框架之LLaMA Factory
算法
echoarts19 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Python技术极客19 小时前
一款超好用的 Python 交互式可视化工具,强烈推荐~
算法
徐小夕20 小时前
花了一天时间,开源了一套精美且支持复杂操作的表格编辑器tablejs
前端·算法·github
小刘鸭地下城20 小时前
深入浅出链表:从基础概念到核心操作全面解析
算法