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

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

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

}


相关推荐
mjhcsp17 小时前
C++ 三分查找:在单调与凸函数中高效定位极值的算法
开发语言·c++·算法
立志成为大牛的小牛17 小时前
数据结构——四十二、二叉排序树(王道408)
数据结构·笔记·程序人生·考研·算法
Funny_AI_LAB19 小时前
李飞飞联合杨立昆发表最新论文:超感知AI模型从视频中“看懂”并“预见”三维世界
人工智能·算法·语言模型·音视频
RTC老炮1 天前
webrtc降噪-PriorSignalModelEstimator类源码分析与算法原理
算法·webrtc
草莓火锅1 天前
用c++使输入的数字各个位上数字反转得到一个新数
开发语言·c++·算法
散峰而望1 天前
C/C++输入输出初级(一) (算法竞赛)
c语言·开发语言·c++·算法·github
Kuo-Teng1 天前
LeetCode 160: Intersection of Two Linked Lists
java·算法·leetcode·职场和发展
fie88891 天前
基于MATLAB的狼群算法实现
开发语言·算法·matlab
偷偷的卷1 天前
【算法笔记 11】贪心策略六
笔记·算法
ZPC82101 天前
FPGA 部署ONNX
人工智能·python·算法·机器人