双向循环链表  和 单向循环链表 查找循环节点 思路都是一样。 快慢指针查找法。 理论可参考

c 链表之 快慢指针 查找循环节点

  1. typedef struct Student_Double
  2. {
  3. char name[];
  4. int point;
  5. struct Student_Double *preStu;
  6. struct Student_Double *nextStu;
  7. } StudentDouble;
  8. StudentDouble * CreateDoubleCircleLink_Table(){
  9.  
  10. int i = ;
  11. StudentDouble *head = NULL;
  12. head=(StudentDouble *)malloc(sizeof(StudentDouble));
  13. head->name[]='\0';
  14. head->point = ;
  15. head->nextStu = NULL;
  16. head->preStu = NULL;
  17.  
  18. //循环节点
  19. StudentDouble *cirleStu = NULL;
  20. StudentDouble *temp = NULL;
  21. StudentDouble *currentNode = head;
  22. while (i<=) {
  23. temp = (StudentDouble *)malloc(sizeof(StudentDouble));
  24. strncpy(temp->name,"Node",sizeof(temp->name));
  25. temp->point = i;
  26. temp->nextStu = NULL;
  27. temp->preStu = currentNode;
  28.  
  29. currentNode->nextStu = temp;
  30. currentNode = temp;
  31.  
  32. if (i==) {
  33. cirleStu = currentNode;
  34. }
  35. i++;
  36. }
  37. //最后 合并循环节点
  38. currentNode->nextStu=cirleStu;
  39. return head;
  40. }
  41. //已知循环节点情况查询循环 链表,验证是否可用
  42. void SelectDoubleLinkTable(StudentDouble *student){
  43. StudentDouble *next = student->nextStu;
  44. int i = ;
  45. StudentDouble *circleStu = NULL;
  46. while (next) {
  47. if (circleStu!=NULL&&next->point == circleStu->point) {
  48. printf("循环节点%d,结束循环\n",next->point);
  49. break;
  50. }
  51. if (i==) {
  52. circleStu = next;
  53. }
  54. printf("index %d; studentName is %s; point is %d\n",i,next->name,next->point);
  55. i++;
  56. next = next->nextStu;
  57. }
  58.  
  59. }
  60. //未知情况查询循环节点
  61. StudentDouble * SelectCircleNodeInDoubleLinkTable(StudentDouble *head){
  62. //快慢指针查询
  63. StudentDouble *fast = head;
  64. StudentDouble *slow = head;
  65.  
  66. while (fast) {
  67. fast = fast->nextStu->nextStu;
  68. slow = slow->nextStu;
  69.  
  70. if (fast==NULL) {//不是循环链表推出
  71. break;
  72. }
  73. if (fast==slow) {//快慢指针相遇
  74. break;
  75. }
  76. }
  77. if (fast == NULL) {
  78. printf("该链表 不是循环链表\n");
  79. return NULL;
  80. }
  81.  
  82. //查找循环节点
  83. fast = head;
  84. while (fast!=slow) {
  85. fast=fast->nextStu;
  86. slow=slow->nextStu;
  87. }
  88. printf("=====找到循环链表循环节点为%d\n",fast->point);
  89. return fast;
  90. }
  91. int main(void){
  92. char sf[];
  93. //创建双向循环链表
  94. StudentDouble *head = NULL;
  95.  
  96. printf("创建双向循环链表Y|N\n");
  97. scanf("%s",sf);
  98. if (strcmp(sf,"Y")==) {
  99. head = CreateDoubleCircleLink_Table();
  100. }
  101. printf("已知情况查询循环链表Y|N \n");
  102. scanf("%s",sf);
  103. if (strcmp(sf,"Y")==) {
  104. SelectDoubleLinkTable(head);
  105. }
  106. printf("未知情况查询循环链表Y|N \n");
  107. scanf("%s",sf);
  108. if (strcmp(sf,"Y")==) {
  109. SelectCircleNodeInDoubleLinkTable(head);
  110. }
  111. return ;
  112. }

复习下C 链表操作(双向循环链表,查找循环节点)的更多相关文章

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

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

  2. 复习下C 链表操作(双向链表)

    双向链表 创建.删除.反转.插入 //struct #include <stdio.h> #include <stdlib.h> #include <string.h&g ...

  3. c 链表之 快慢指针 查找循环节点

    参考:http://blog.csdn.net/wenqian1991/article/details/17452715 上面分析了 根据这张图 推倒出 数学公式. 刚接触 不能一下弄明白.下面结合上 ...

  4. c 链表之 快慢指针 查找循环节点(转)

    上面分析了 根据这张图 推倒出 数学公式. 刚接触 不能一下弄明白.下面结合上面文章的分析.仔细推倒一下 , 一般设置 快指针 速度是 慢指针的2倍.及 快指针每次遍历两个指针, 慢指针每次遍历1个指 ...

  5. 复习下C 链表操作(单向链表)

    Object-C 作为C 的包装语言(运行时.消息机制).如果不熟悉C 的话实在玩得太肤浅. 随便深入oc 内部都会接触到C. runtime .GCD.Block.消息机制... 所有强大的功能无不 ...

  6. linux下的文本操作之 文本查找——grep

    摘要:你有没有这样的应用场景:调试一个程序,出现debug的提示信息,现在你需要定位是哪个文件包含了这个debug信息,也就是说,你需要在一个目录下的多个文件(可能包含子目录)中查找某个字符串的位置: ...

  7. JAVA 链表操作:循环链表

    主要分析示例: 一.循环链表简述 二.单链表循环链表 三.双链表循环链表 一.循环链表简述 循环链表即链表形成了一个循环的结构,尾节点不再指向NULL,而是指向头节点HEAD,此时判定链表的结束是尾节 ...

  8. 后台开发 3个题目 array_chunk, 100块钱找零钱(动态规划 dynamic programming), 双向循环链表 llist 删除节点

    1. array_chunk 实现 http://php.net/manual/en/function.array-chunk.php <?php function my_array_chunk ...

  9. linked-list-cycle-ii——链表,找出开始循环节点

    Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...

随机推荐

  1. 【Java】 剑指offer(15) 数值的整数次方

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 实现函数double Power(double base, int ...

  2. Scrapy爬虫笔记 - 爬取知乎

    cookie是一种本地存储机制,cookie是存储在本地的 session其实就是将用户信息用户名.密码等)加密成一串字符串,返回给浏览器,以后浏览器每次请求都带着这个sessionId 状态码一般是 ...

  3. springboot Autowired BeanNotOfRequiredTypeException

    现象 org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxxxImpl' is expect ...

  4. 生产环境中tomcat的配置

    生产环境中要以daemon方式运行tomcat 通常在开发环境中,我们使用$CATALINA_HOME/bin/startup.sh来启动tomcat, 使用$CATALINA_HOME/bin/sh ...

  5. 搞IT,算法编程不错的学习网址 & 一些专栏博客大神的地址(汇总)

    博客专栏大神 王晓华(算法的乐趣) 算法系列:http://blog.csdn.net/orbit/article/category/830251 PostgreSQL深入理解内核系列:http:// ...

  6. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

  7. 菜鸟nginx源代码剖析数据结构篇(八) 缓冲区链表ngx_chain_t

    菜鸟nginx源代码剖析数据结构篇(八) 缓冲区链表 ngx_chain_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog. ...

  8. UIView的层次结构–code

    转:http://blog.dongliwei.cn/archives/uiview-tree-code // Recursively travel down the view tree, incre ...

  9. http://blog.csdn.net/wzzvictory/article/details/16994913

      原文地址:http://blog.csdn.net/wzzvictory/article/details/16994913   一.什么是instancetype instancetype是cla ...

  10. Java断言绝对不是鸡肋

    在防御式编程中经常会用断言(Assertion)对参数和环境做出判断,避免程序因不当的输入或错误的环境而产生逻辑异常,断言在很多语言中都存在,C.C++.Python 都有不同的断言表示形式.在Jav ...