当前位置: 首页 > 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.rkmt.cn/news/54417.html

相关文章:

  • 2025 年 11 月高温老化房厂家推荐排行榜,老化室、高温老化室、高温房、熟化房、固化房、恒温恒湿室、恒温房、恒温恒湿房公司推荐
  • 2025 年 11 月耐磨板厂家推荐排行榜,国产耐磨板,悍达耐磨板,堆焊耐磨板,进口耐磨板,MN13耐磨板,NM360-NM600高强度耐磨板,高铬合金耐磨板公司推荐!
  • 视频融合平台EasyCVR助力守护渔业牧区安全与增效
  • 2025 最新推荐!精雕机厂家口碑排行榜,国际协会测评认证 + 多行业适配实力权威发布高校合作精雕机/东莞精雕机/广东精雕机公司推荐
  • 2025 最新供水设备源头厂家推荐排行榜:无负压 / 恒压 / 变频供水设备实力品牌精选
  • 2025 年智慧停车系统、高校智慧停车系统十大品牌权威推荐!破解停车难题,这些优质品牌值得选择
  • 实用指南:智慧家政系统:未来家庭管理的核心技术解析
  • 【广东工业大学东莞理工学院联合主办,IEEE出版】第六届机械工程、智能制造与自动化技术国际学术会议(MEMAT 2025)
  • 给Snipe-IT添加扫码盘点
  • Sass 入门
  • 2025年平面精铣机制造厂权威推荐榜单:数控四头铣床/卧式粗框机/龙门磨床源头厂家精选
  • 为PDP-11/44安装84MB硬盘的复古计算实践
  • day13_mvc 前后端分离 - 教程
  • CF1221F Choose a Square
  • Python pip使用简介和国内镜像下载
  • 2025-11-19 ZYZ28-NOIP-XiaoMao Round 33550336 hetao1733837的record
  • 02.入门篇-开发工具
  • 2025年防水贴缝带实力厂家权威推荐榜单:双面贴缝带/单面贴缝带/道路贴缝带源头厂家精选
  • AT AGC004 题解【鸽】
  • 【Java Web学习 | 第1篇】前端 - HTML - 详解
  • 2025年连续梁防落梁装置源头厂家权威推荐榜单:防落梁装置施工/防落梁装置缓冲器/链环式防落梁装置源头厂家精选
  • 不同行业企业如何选择可观测产品?
  • 2025年可观测厂商解析:博睿数据如何领跑全球可观测性市场?
  • Joycode 无法跨项目读取源码怎么办?MCP Easy Code Reader 帮你解决!
  • Codes 创新的低代码接口测试解决方案,让点工也能做好接口自动化测试且效率起飞
  • 2025年均质乳化机订制厂家权威推荐榜单:分散乳化机/管线式乳化机/乳化设备源头厂家精选
  • GAN生成式对抗网络
  • OBET工具使用说明
  • 2025年重庆科技展示展厅公司权威推荐榜单:博物馆数字展厅/科技展馆/智能全息展馆源头公司精选
  • java泛型类型通配符