当前位置: 首页 > news >正文

单向循环链表接口设计

单向循环链表接口设计

  • @file name: 单向循环链表接口设计
  • @brief :设计单向循环链表,实现各种功能函数并测试
  • @author m15629473867@163.com
  • @date 2025/11/19
  • @version 1.0
  • @property
  • @note
  • CopyRight (c) 2025-2026 m15629473867@163.com All Right Reseverd

构造单向循环链表结构体

// 指的是单向循环链表中的结点有效数据类型,用户可以根据需要进行修改
typedef int DataType_t;// 构造单向循环链表的结点,链表中所有结点的数据类型应该是相同的
typedef struct CircularLinkedList
{DataType_t data;				 // 结点的数据域struct CircularLinkedList *next; // 结点的指针域} CircLList_t;

创建一个空单向循环链表并初始化

CircLList_t *CircLList_Create()
{// 1.创建一个头结点并对头结点申请内存CircLList_t *Head = (CircLList_t *)calloc(1, sizeof(CircLList_t));if (NULL == Head){perror("Calloc memory for Head is Failed");exit(-1);}// 2.对头结点进行初始化,头结点是不存储数据域,指针域指向自身,体现“循环”思想Head->next = Head;return Head; // 3.把头结点的地址返回即可
}

创建新的结点,并对新结点进行初始化

CircLList_t *CircLList_NewNode(DataType_t data)
{// 1.创建一个新结点并对新结点申请内存CircLList_t *New = (CircLList_t *)calloc(1, sizeof(CircLList_t));if (NULL == New){perror("Calloc memory for NewNode is Failed");return NULL;}// 2.对新结点的数据域和指针域进行初始化New->data = data;New->next = NULL;return New;
}

功能函数:从首节点进行插入元素

bool CircLList_HeadInsert(CircLList_t *Head, DataType_t data)
{CircLList_t *new = CircLList_NewNode(data);CircLList_t *tmp = Head->next;if (Head->next == Head) // empty list{Head->next = new;new->next = new;return true;}while (tmp->next != Head->next) // normal situation,find the last nodetmp = tmp->next;new->next = Head->next;Head->next = new;tmp->next = new;return true;
}

功能函数:从尾部插入新元素

bool CircLList_TailInsert(CircLList_t *Head, DataType_t data)
{CircLList_t *new = CircLList_NewNode(data);if (Head->next == Head) // judge is the null{Head->next = new;new->next = new;return true;}CircLList_t *tmp;while (tmp->next != Head->next) // when the normal situation,find the last nodetmp = tmp->next;tmp->next = new;new->next = Head->next;return true;
}

功能函数:从指定位置插入新元素

bool CircLList_DestInsert(CircLList_t *Head, DataType_t destval, DataType_t data)
{CircLList_t *tmp = Head->next;DataType_t i = Head->data;if (Head->next == Head) // judge the empty list{printf("The list is empty");return false;}CircLList_t *new = CircLList_NewNode(data);while (destval != tmp->data && tmp->next != Head->next){tmp = tmp->next;}if (destval == tmp->data){new->next = tmp->next;tmp->next = new;return true;}else{printf("There is no destval\n");return false;}
}

功能函数:遍历打印链表

bool CircLList_Print(CircLList_t *Head)
{// 对单向循环链表的头结点的地址进行备份CircLList_t *Phead = Head;// 判断当前链表是否为空,为空则直接退出if (Head->next == Head){printf("current linkeflist is empty!\n");return false;}// 从首结点开始遍历while (Phead->next){// 把头结点的直接后继作为新的头结点Phead = Phead->next;// 输出头结点的直接后继的数据域printf("data = %d\n", Phead->data);// 判断是否到达尾结点,尾结点的next指针是指向首结点的地址if (Phead->next == Head->next){break;}}return true;}

功能函数:删除首节点

bool CircLList_HeadDel(CircLList_t *Head)
{// 对单向循环链表的头结点的地址进行备份CircLList_t *Phead = Head->next;CircLList_t *tmp = Head->next;if (Head->next == Head) // 判断当前链表是否为空,为空则直接退出{printf("current linkeflist is empty!\n");return false;}while (tmp->next != Head->next) // find the last nodetmp = tmp->next;tmp->next = Phead->next;Head->next = Phead->next;Phead->next = NULL;free(Phead);return true;
}

功能函数:删除尾部节点

bool CircLList_TailDel(CircLList_t *Head)
{CircLList_t *tmpFormer;CircLList_t *tmp = Head->next;if (Head->next == Head) // 判断当前链表是否为空,为空则直接退出{printf("current linkeflist is empty!\n");return false;}while (tmp->next != Head->next) // find the last node{tmpFormer = tmp;tmp = tmp->next;}tmpFormer->next = Head->next;tmp->next = NULL;free(tmp);return true;}

功能函数:删除指定位置的节点

bool CircLList_MidDel(CircLList_t *Head, DataType_t destval)
{CircLList_t *tmpFormer;CircLList_t *tmp = Head->next;if (Head->next == Head) // 判断当前链表是否为空,为空则直接退出{printf("current linkeflist is empty!\n");return false;}while (tmp->data != destval && tmp->next != Head->next) // find the specific node{tmpFormer = tmp;tmp = tmp->next;}if (tmp->data == destval){tmpFormer->next = tmp->next;tmp->next = NULL;free(tmp);return true;}else{printf("The is no destival\n");return false;}

主函数,调用并测试各功能函数

int main()
{CircLList_t *H = CircLList_Create();CircLList_HeadInsert(H, 10);CircLList_HeadInsert(H, 20);CircLList_HeadInsert(H, 30);CircLList_TailInsert(H, 30);CircLList_TailInsert(H, 40);CircLList_DestInsert(H, 30, 15);CircLList_Print(H);puts("");CircLList_HeadDel(H);CircLList_Print(H);puts("");CircLList_TailDel(H);CircLList_Print(H);puts("");CircLList_MidDel(H, 12);CircLList_Print(H);return 0;
}
http://www.jsqmd.com/news/44831/

相关文章:

  • 2025 最新雕刻机源头厂家权威推荐榜:自主研发专利加持 + 国际测评认证,高精度设备优选清单数控雕刻机/激光雕刻机/小型雕刻机/金属雕刻机公司推荐
  • linux firewall
  • 2025 年 11 月高温老化房厂家推荐排行榜,老化室、高温老化室、高温房、熟化房、固化房、恒温恒湿室、恒温房、恒温恒湿房公司推荐
  • 2025 年 11 月耐磨钢板厂家推荐排行榜,高耐磨钢板,高锰耐磨钢板,焊达500耐磨钢板,进口复合耐磨钢板,双金属复合耐磨钢板公司推荐
  • 2025 年 11 月耐磨板厂家推荐排行榜,国产耐磨板,悍达耐磨板,堆焊耐磨板,进口耐磨板,MN13耐磨板,NM360-NM600高强度耐磨板,高铬合金耐磨板公司推荐!
  • 视频融合平台EasyCVR助力守护渔业牧区安全与增效
  • 2025 最新推荐!精雕机厂家口碑排行榜,国际协会测评认证 + 多行业适配实力权威发布高校合作精雕机/东莞精雕机/广东精雕机公司推荐
  • 2025 最新供水设备源头厂家推荐排行榜:无负压 / 恒压 / 变频供水设备实力品牌精选
  • 2025 最新限制器厂家权威推荐榜:国际测评认证 + 年产超 20 万套实力品牌,生产与服务全方位评估展现卓越制造能力限制器/扭力限制器/扭矩限制器公司推荐
  • 2025 年安全联轴器厂家最新推荐榜单:权威测评优质厂家 ,铸就传动安全与品质保障标杆
  • 2025 年智慧停车系统、高校智慧停车系统十大品牌权威推荐!破解停车难题,这些优质品牌值得选择
  • 实用指南:智慧家政系统:未来家庭管理的核心技术解析
  • 【广东工业大学东莞理工学院联合主办,IEEE出版】第六届机械工程、智能制造与自动化技术国际学术会议(MEMAT 2025)
  • 给Snipe-IT添加扫码盘点
  • 2025 最新推荐沈阳阳光房厂家实力榜单:国际协会测评认证 + 专利技术加持,20000 平厂房保障品质与交付沈阳阳光房公司推荐
  • 2025 最新电磁灶厂家权威推荐榜:聚焦商用大功率 / 智能款,国际测评认证口碑实力双优品牌合集商用多头/商用智能/SUKIO/3500 瓦大功率/SUKI0/硕高电磁灶公司推荐
  • 移动端反射探针格式用什么比较合理
  • linux find 删除
  • 完整教程:Navicat - 连接 mysql 、 sqlserver 数据库 步骤与问题解决
  • 2025年平衡重制造企业权威推荐榜单:平衡块订做/后平衡铁工厂/尾部配重铁源头厂家精选
  • 03.入门篇-集成开发环境
  • 2025 最新祛斑加盟招商品牌权威推荐榜:国际协会测评认证 + 全链条品质标杆,创业加盟优选指南
  • 2025 激光切割机厂家 TOP5 推荐:高精度技术标杆 + 全场景适配,选型不踩坑!
  • Sass 入门
  • F28379D launchpad学习记录——实时音频采集:INMP441数字MEMS麦克风为例
  • day13_mvc 前后端分离 - 教程
  • noexcept修饰的函数无法被catch到(只会崩溃)
  • C语言的strlen的模拟实现
  • CF1221F Choose a Square
  • 2025年pp阻燃管制造企业权威推荐榜单:pp灌溉管/pp管件批发/pp聚丙烯管源头厂家精选