鱼C论坛

 找回密码
 立即注册
查看: 2852|回复: 1

[技术交流] 关于判断单链表是否有环代码的分享

[复制链接]
发表于 2020-2-27 18:18:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
vscode写的

有些英语单词拼写错误,大家参考参考



  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct people{
  4.     int numb;
  5.     struct people* nuxt;
  6. }people;

  7. //初始化有环的链表:
  8. //自己创建一个有环或者没环的链表
  9. people *create_struct()
  10. {
  11.     int size = sizeof(people);
  12.     people *a1,*a2,*a3,*a4,*a5,*a6;
  13.     a1 = (people *)malloc(size);
  14.     a2 = (people *)malloc(size);
  15.     a3 = (people *)malloc(size);
  16.     a4 = (people *)malloc(size);
  17.     a5 = (people *)malloc(size);
  18.     a6 = (people *)malloc(size);
  19.     a1->nuxt = a2;
  20.     a2->nuxt = a3;
  21.     a3->nuxt = a4;
  22.     a4->nuxt = a5;
  23.     a5->nuxt = a6;
  24.     a6->nuxt = a3;        //这个相当于创建有环
  25.     //a6->nuxt = NULL;   这个相当于创建无环
  26.     return a1;
  27. }


  28. //判断单链表是否有环
  29. //第一种方法
  30. //第一指针先一步一步走
  31. //第二指针每次第一步开始走
  32. //存在环时   第一指针返回  当与第二指针开始相碰时则证明环存在
  33. //传入单链表的头结点指针  和 链表长度  比如自己上面创建的链表长度为6
  34. int test1(people *test,int lenth)
  35. {
  36.     int numb=1;
  37.     people *head,*read;
  38.     read = head = test;
  39.     while(lenth != 0)
  40.     {
  41.         int buchang=0;
  42.         head = test;
  43.         read = read->nuxt;
  44.         for (int i=0;i<numb;i++)
  45.         {
  46.             buchang++;
  47.            // printf("buchang%d\n",buchang);
  48.            // printf("numb%d\n",numb);
  49.           //  printf("head = %p\n",head);
  50.            // printf("read = %p\n",read);
  51.             if(head==read && buchang<numb)
  52.                 {
  53.                     printf("方法1判断:存在,");
  54.                     printf("环的节点在%d处\n",buchang);
  55.                     return 0;
  56.                 }
  57.             head = head->nuxt;
  58.         }
  59.         numb++;
  60.         lenth--;
  61.     }
  62.     printf("\n方法1判断:不存在\n");
  63.     return 1;
  64. }


  65. //第二种  快慢指针的方法判断是否有环  
  66. //快指针速度是慢指针速度的两倍
  67. //当存在环时   快指针总有一刻会绕到慢指针  而后与慢指针相撞则证明环存在
  68. //传入链表头结点指针
  69. int test2(people *test)
  70. {
  71.     people *spead1=test,*spead2=test,*test11 = 0;
  72.     while (test11 != spead2 )
  73.     {
  74.          
  75.         spead1 = spead1->nuxt;
  76.         spead2 = spead2->nuxt;
  77.         if(spead2==NULL)
  78.         {
  79.             printf("方法2判断:不存在\n");
  80.             return 0;
  81.         }
  82.         spead2 = spead2->nuxt;
  83.         if(spead2==NULL)
  84.         {
  85.             printf("方法2判断:不存在\n");
  86.             return 0;
  87.         }
  88.         if (spead1 == spead2)
  89.         {
  90.             printf("方法2判断:存在\n");
  91.             return 1;
  92.         }   
  93.         test11 = spead1;

  94.     }
  95.    
  96. }

  97. int main()
  98. {
  99.     people *test0;
  100.     test0 = create_struct();
  101.     test1(test0,6);
  102.     test2(test0);
  103.     return 0;
  104. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-2-27 18:20:29 | 显示全部楼层
当:a6->nuxt = NULL;
为无环链表时,

运行结果:
方法1判断:不存在
方法2判断:不存在





当:a6->nuxt = a4;
为有环链表时

运行结果:
方法1判断:存在,环的节点在4处
方法2判断:存在


小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 11:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表