头文件:

  1. #ifndef _CIRCLELIST_H_
  2. #define _CIRCLELIST_H_
  3. typedef void CircleList;
  4.  
  5. //
  6.  
  7. typedef struct _tag_CircleListNode
  8. {
  9. struct _tag_CircleListNode* next;
  10. }CircleListNode;
  11.  
  12. //创建一个循环链表
  13. CircleList* CircleList_Create();
  14. //删除一个循环链表
  15. void CircleList_Destroy(CircleList* list);
  16. //清空一个循环链表
  17. void CircleList_Clear(CircleList* list);
  18. //返回链表的长度
  19. int CircleList_Length(CircleList* list);
  20. //在POS位置插入一个节点
  21. int CircleList_Insert(CircleList* list, CircleListNode* node, int pos);
  22. //获取POS位置节点的信息
  23. CircleListNode* CircleList_Get(CircleList* list, int pos);
  24. //删除POS位置的节点
  25. CircleListNode* CircleList_Delete(CircleList* list, int pos);
  26.  
  27. ////与游标相关的函数
  28. //删除游标所指的位置节点
  29. CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node);
  30. //重置游标位置
  31. CircleListNode* CircleList_Reset(CircleList* list);
  32. //当前游标位置
  33. CircleListNode* CircleList_Current(CircleList* list);
  34. //游标的NEXT域
  35. CircleListNode* CircleList_Next(CircleList* list);
  36.  
  37. #endif

CPP文件:

  1. #include "circleList.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. //这个为头链表头
  7. typedef struct _tag_CircleList
  8. {
  9. CircleListNode header;
  10. CircleListNode* slider;
  11. int length;
  12. }tagCircleList;
  13.  
  14. //创建一个循环链表
  15. CircleList* CircleList_Create()
  16. {
  17. tagCircleList* ret = (tagCircleList*)malloc(sizeof(tagCircleList)); //分配内存
  18. if (ret == NULL)
  19. {
  20. return NULL;
  21. }
  22.  
  23. //初始化
  24. ret->header.next = NULL;
  25. ret->length = ;
  26. ret->slider = NULL;
  27.  
  28. return ret;
  29. }
  30.  
  31. //删除一个循环链表
  32. void CircleList_Destroy(CircleList* list)
  33. {
  34. if (list = NULL)
  35. {
  36. return;
  37. }
  38. //释放内存
  39. free(list);
  40. return;
  41. }
  42.  
  43. //清空一个循环链表
  44. void CircleList_Clear(CircleList* list)
  45. {
  46. tagCircleList* sList = NULL;
  47. sList = (tagCircleList*)list;
  48. if (sList == NULL)
  49. {
  50. return ;
  51. }
  52. //重置为初始化状态
  53. sList->header.next = NULL;
  54. sList->length = ;
  55. sList->slider = NULL;
  56. return;
  57. }
  58.  
  59. //返回链表的长度
  60. int CircleList_Length(CircleList* list)
  61. {
  62. tagCircleList* sList = NULL;
  63. sList = (tagCircleList*)list;
  64. int ret = -;
  65. //异常处理
  66. if (list == NULL)
  67. {
  68. return ret;
  69. }
  70.  
  71. return sList->length;
  72. }
  73.  
  74. //在POS位置插入一个节点
  75. int CircleList_Insert(CircleList* list, CircleListNode* node, int pos)
  76. {
  77. tagCircleList* sList = NULL;
  78. sList = (tagCircleList*)list;
  79. int ret = -;
  80. //异常处理
  81. if(list == NULL || node == NULL || pos<)
  82. {
  83. return ret;
  84. }
  85. //临时变量Current
  86. CircleListNode* Current = (CircleListNode*)sList;
  87.  
  88. for(int i = ; (i < pos) && (Current->next != NULL); i++)
  89. {
  90. Current = Current->next;
  91. }
  92.  
  93. node->next = Current->next;
  94. Current->next = node;
  95.  
  96. //当长度为0时 游标指向node
  97. if (sList->length == )
  98. {
  99. sList->slider = node;
  100. }
  101.  
  102. sList->length++;
  103. //如果current 依旧指向链表头 证明没跳走 是从0开始插入的 需要头插法
  104. if (Current == (CircleListNode*)sList)
  105. {
  106. //定义一个辅助last 变量来获取尾部节点的信息
  107. CircleListNode* last = (CircleListNode*)CircleList_Get(sList, sList->length - );
  108. //将尾部节点的NEXT域存为当前节点(头节点)
  109. last->next = Current->next;
  110. }
  111. return ;
  112. }
  113.  
  114. //获取POS位置节点的信息
  115. CircleListNode* CircleList_Get(CircleList* list, int pos)
  116. {
  117.  
  118. tagCircleList* sList = (tagCircleList*)list;
  119. CircleListNode* ret = NULL;
  120. int i = ;
  121. if (list == NULL || pos < )
  122. {
  123. return NULL;
  124. }
  125. CircleListNode* Current = (CircleListNode*)sList;
  126. for(i = ; i < pos; i++)
  127. {
  128. Current = Current->next;
  129. }
  130.  
  131. ret = Current->next;
  132. return ret;
  133. }
  134.  
  135. //删除POS位置的节点
  136. CircleListNode* CircleList_Delete(CircleList* list, int pos)
  137. {
  138. tagCircleList* sList = (tagCircleList*)list;
  139. CircleListNode* ret = NULL;
  140.  
  141. if ((sList != NULL) && (pos >=) && (sList->length > ))
  142. {
  143. //将Current指向表头
  144. CircleListNode* Current = (CircleListNode*)(&(sList->header));
  145. //辅助节点last 进行头节点的删除使用 存取最后一个元素
  146. CircleListNode* last = NULL;
  147.  
  148. for(int i = ; i < pos; i++)
  149. {
  150. Current = Current->next;
  151. }
  152. //删除头结点
  153. if ( Current == (CircleListNode*)sList)
  154. {
  155. last = (CircleListNode*)CircleList_Get(sList, sList->length - );
  156. }
  157. //要删除的元素
  158. ret = Current->next;
  159. Current->next = ret->next;
  160. sList->length--;
  161.  
  162. //判断链表非空
  163. if( last != NULL)
  164. {
  165. //sList->header.next = ret->next;
  166. Current->next = ret->next;
  167. last->next = ret->next;
  168. }
  169. //若删除的元素为游标所指的元素
  170. if(sList->slider = ret)
  171. {
  172. sList->slider = ret->next;
  173. }
  174. //若删除元素后 链表长度为0 做处理
  175. if (sList->length == )
  176. {
  177. sList->header.next = NULL;
  178. sList->slider = NULL;
  179. }
  180. }
  181. return ret;
  182. }
  183.  
  184. ////与游标相关的函数
  185. //删除游标所指的位置节点
  186. CircleListNode* CircleList_DeleteNode(CircleList* list, CircleListNode* node)
  187. {
  188. tagCircleList* sList = (tagCircleList*)list;
  189. CircleListNode* ret = NULL;
  190. int i = ;
  191.  
  192. if (sList != NULL)
  193. {
  194. CircleListNode* Current = (CircleListNode*)sList;
  195. //循环查找node 在链表中的位置
  196. for (i = ; i < sList->length; i++)
  197. {
  198. if (Current->next == node)
  199. {
  200. ret = Current->next;
  201. break;
  202. }
  203.  
  204. Current = Current->next;
  205. }
  206. //找到了 使用CircleList_Delete 删除
  207. if(ret != NULL)
  208. {
  209. CircleList_Delete(list, i);
  210. }
  211.  
  212. }
  213.  
  214. return ret;
  215. }
  216.  
  217. //重置游标位置
  218. CircleListNode* CircleList_Reset(CircleList* list)
  219. {
  220. tagCircleList* sList = (tagCircleList*)list;
  221. CircleListNode* ret = NULL;
  222. //如果不为空
  223. if (sList != NULL)
  224. {
  225. sList->slider = sList->header.next;
  226. ret = sList->slider;
  227. }
  228.  
  229. return ret;
  230. }
  231.  
  232. //当前游标位置
  233. CircleListNode* CircleList_Current(CircleList* list)
  234. {
  235. tagCircleList* sList = (tagCircleList*)list;
  236. CircleListNode* ret = NULL;
  237. //如果不为空
  238. if (sList != NULL)
  239. {
  240. ret = sList->slider;
  241. }
  242.  
  243. return ret;
  244. }
  245.  
  246. //把游标位置返回,游标下移
  247. CircleListNode* CircleList_Next(CircleList* list)
  248. {
  249. tagCircleList* sList = (tagCircleList*)list;
  250. CircleListNode* ret = NULL;
  251. //如果不为空
  252. if((sList != NULL) && (sList->slider != NULL))
  253. {
  254. ret = sList->slider;
  255. sList->slider = ret->next;
  256. }
  257.  
  258. return ret;
  259. }

测试函数:

  1. #include "circleList.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. typedef struct _Temp_Test
  7. {
  8. CircleListNode node;
  9. int temp;
  10. char temp2;
  11. }TempTast;
  12.  
  13. int main()
  14. {
  15. CircleList* circlelist = NULL;
  16.  
  17. circlelist = CircleList_Create();
  18. //异常处理
  19. if (circlelist == NULL)
  20. {
  21. cout << "Create Err " << endl;
  22. return -;
  23. }
  24.  
  25. TempTast t1, t2, t3, t4, t5;
  26. t1.temp = ;
  27. t2.temp = ;
  28. t3.temp = ;
  29. t4.temp = ;
  30. t5.temp = ;
  31. //插入元素
  32. CircleList_Insert(circlelist, (CircleListNode*)(&t1), );
  33. CircleList_Insert(circlelist, (CircleListNode*)(&t2), );
  34. CircleList_Insert(circlelist, (CircleListNode*)(&t3), );
  35. CircleList_Insert(circlelist, (CircleListNode*)(&t4), );
  36. CircleList_Insert(circlelist, (CircleListNode*)(&t5), );
  37. //测试功能
  38. cout << "Length: " << CircleList_Length(circlelist) << endl;
  39. //遍历两次
  40. cout << "遍历两次:" << endl;
  41. for(int i = ; i < *CircleList_Length(circlelist); i++)
  42. {
  43. cout <<"Node:" << ((TempTast*)CircleList_Get(circlelist, i))->temp << endl;
  44. }
  45. cout << endl;
  46. //删除第一个节点
  47. cout <<"Node:" << ((TempTast*)CircleList_Delete(circlelist, ))->temp << endl;
  48. //清空
  49. CircleList_Clear(circlelist);
  50. cout << "After Clear Length: " << CircleList_Length(circlelist) << endl;
  51.  
  52. //插入元素
  53. CircleList_Insert(circlelist, (CircleListNode*)(&t1), );
  54. CircleList_Insert(circlelist, (CircleListNode*)(&t2), );
  55. CircleList_Insert(circlelist, (CircleListNode*)(&t3), );
  56. CircleList_Insert(circlelist, (CircleListNode*)(&t4), );
  57. CircleList_Insert(circlelist, (CircleListNode*)(&t5), );
  58. //删除指定元素
  59. cout << "Delete Node :" << ((TempTast*)CircleList_DeleteNode(circlelist, (CircleListNode*)(&t1)))->temp << endl;
  60. //显示游标当前位置
  61. cout << "Silder Now :" << ((TempTast*)CircleList_Current(circlelist))->temp << endl;
  62. //移动后
  63. CircleList_Next(circlelist);
  64. cout << "Silder After Next :" << ((TempTast*)CircleList_Current(circlelist))->temp << endl;
  65. //重置后
  66. CircleList_Reset(circlelist);
  67. cout << "Silder After Reset :" << ((TempTast*)CircleList_Current(circlelist))->temp << endl;
  68. cout << endl;
  69. //销毁
  70. CircleList_Destroy(circlelist);
  71. cout << "circle has been Destroied" << endl;
  72. system("pause");
  73. return ;
  74. }

循环链表的C风格实现(单向)的更多相关文章

  1. python中的单向循环链表实现

    引子 所谓单向循环链表,不过是在单向链表的基础上,如响尾蛇般将其首尾相连,也因此有诸多类似之处与务必留心之点.尤其是可能涉及到头尾节点的操作,不可疏忽. 对于诸多操所必须的遍历,这时的条件是什么?又应 ...

  2. 复习下C 链表操作(单向循环链表、查找循环节点)

    循环链表 稍复杂点. 肯能会有0 或 6 字型的单向循环链表.  接下来创建 单向循环链表 并 查找单向循环链表中的循环节点. 这里已6字型单向循环链表为例. //创建 循环链表 Student * ...

  3. 03-java实现循环链表

    03java实现循环链表 本人git https://github.com/bigeyes-debug/Algorithm 一丶单向循环链表 就是为尾节点指向头结点 二丶单向循环链表的接口设计 比较单 ...

  4. Java集合源码分析(三)LinkedList

    LinkedList简介 LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈.队列和双端队列来使用. LinkedList同样是非线程安全 ...

  5. 数据结构Java实现04----循环链表、仿真链表

    单向循环链表 双向循环链表 仿真链表 一.单向循环链表: 1.概念: 单向循环链表是单链表的另一种形式,其结构特点是链表中最后一个结点的指针不再是结束标记,而是指向整个链表的第一个结点,从而使单链表形 ...

  6. 基本数据结构:链表(list)

    copy from:http://www.cppblog.com/cxiaojia/archive/2012/07/31/185760.html 基本数据结构:链表(list) 谈到链表之前,先说一下 ...

  7. 用Python实现的数据结构与算法:链表

    一.概述 链表(linked list)是一组数据项的集合,其中每个数据项都是一个节点的一部分,每个节点还包含指向下一个节点的链接(参考 <算法:C语言实现>). 根据结构的不同,链表可以 ...

  8. Python实现的数据结构与算法之链表详解

    一.概述 链表(linked list)是一组数据项的集合,其中每个数据项都是一个节点的一部分,每个节点还包含指向下一个节点的链接.根据结构的不同,链表可以分为单向链表.单向循环链表.双向链表.双向循 ...

  9. 基于visual Studio2013解决算法导论之021单向循环链表

     题目 单向循环链表的操作 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> ...

随机推荐

  1. YII报错笔记:<pre>PHP Notice &#039;yii\base\ErrorException&#039; with message &#039;Uninitialized string offset: 0&#039; in /my/test/project/iot/vendor/yiisoft/yii2/base/Model.php:778

    YII常见报错笔记 报错返回的代码如下: <pre>PHP Notice 'yii\base\ErrorException' with message 'Uninitialized str ...

  2. python 生成器与迭代器

    #! /usr/bin/env python# -*- coding:utf-8 -*- def xrange(n): num = 0 while True: if num > n: retur ...

  3. BS3 多级菜单

    <div class="container"> <div class="row"> <h2>Multi level drop ...

  4. Apache is running a threaded MPM, but your PHP module is not compiled to be threadsafe. you need to recompile php. pre-configuration failed

    手动配置想要组合版本的wamp环境时,在服务器上直接下载的几个安装包怎么都组合安装不成功,纠结很久,终于找到原因.配置apache支持php后apache一直无法成功启动.后来发现php是nts的版本 ...

  5. 7、斐波那契数列、跳台阶、变态跳台阶、矩形覆盖------------>剑指offer系列

    题目:斐波那契数列 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). f(n) = f(n-1) + f(n-2) 基本思路 这道题在剑指offe ...

  6. 洛谷 P1873 砍树

    砍树 二分答案,难度较低. #include <iostream> #include <cstdio> #include <algorithm> using nam ...

  7. 安卓4.4不支持touchend事件解决办法

    最近的项目要求兼容到OPPO A31这款手机,这款手机是安卓4.4,调试时遇到了touch手指不能滑动页面切换的问题,最终解决通过在touchstart事件里面加上一个 event.preventDe ...

  8. 16年毕业的前端er在杭州求职ing

    来杭州也有一两个星期了,这个周末下雨,是在没地去,还是习惯性的打开电脑逛技术论坛,想想也是好久没有更新博文了... 背景 因为曾经看过一篇文章面试分享:一年经验初探阿里巴巴前端社招所以来杭州也是带有目 ...

  9. spark基准测试-BigDataBenchs

    https://blog.csdn.net/haoxiaoyan/article/details/53895068

  10. Selenium私房菜系列6 -- 深入了解Selenium RC工作原理(1)

    前一篇已经比较详细讲述了如何使用Selenium RC进行Web测试,但到底Selenium RC是什么?或者它由哪几部分组成呢?? 一.Selenium RC的组成: 关于这个问题,我拿了官网上的一 ...