尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

单向循环链表接口设计

单向循环链表接口设计
📅 发布时间:2026/6/19 2:42:23

单向循环链表接口设计

  • @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;
}

相关新闻

  • 2025 年 11 月高温老化房厂家推荐排行榜,老化室、高温老化室、高温房、熟化房、固化房、恒温恒湿室、恒温房、恒温恒湿房公司推荐
  • 2025 年 11 月耐磨板厂家推荐排行榜,国产耐磨板,悍达耐磨板,堆焊耐磨板,进口耐磨板,MN13耐磨板,NM360-NM600高强度耐磨板,高铬合金耐磨板公司推荐!
  • 视频融合平台EasyCVR助力守护渔业牧区安全与增效

最新新闻

  • 如何构建高效的股票智能分析系统:自动化部署与配置指南
  • DeepSeek V4双模架构解析:1M上下文与OPD训练的工程化落地
  • 2026目前最好的数字展厅全彩屏厂家怎么选 - 品牌排行榜
  • 98. 从单核到集群:如何评估与规划服务的QPS承载能力
  • 2026年苏州专攻离婚房产分割的律师选择参考 - 品牌排行榜
  • DeepSeek-V4高效长上下文推理技术解析

日新闻

  • 信任的进化:技术实现详解——如何用JavaScript构建博弈论模拟器
  • Terrakube自定义工作流:如何集成OPA、Infracost等工具扩展IaC能力
  • grunt-concurrent快速入门:5分钟学会并行运行Grunt任务

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号