单链表是否有环及环入口点

Posted hemeiwolong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单链表是否有环及环入口点相关的知识,希望对你有一定的参考价值。

参考与原理:https://www.jianshu.com/p/ef71e04241e4

https://blog.csdn.net/danny_idea/article/details/89504629

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 typedef struct node pnode;
  5 struct node
  6 {
  7     int info;
  8     pnode *next;
  9 };
 10 
 11 void create(pnode **head)   //创建表头
 12 {
 13     (*head) = (pnode *)malloc(sizeof(pnode));
 14     (*head)->info = 0;
 15     (*head)->next = nullptr;
 16 }
 17 
 18 void add(pnode *head, int val)  //添加节点
 19 {
 20     head->info ++;
 21 
 22     while (head->next != nullptr)
 23         head = head->next;
 24 
 25     pnode *temp = (pnode *)malloc(sizeof(pnode));
 26     temp->info = val;
 27     temp->next = nullptr;
 28     head->next = temp;
 29 }
 30 
 31 void show(pnode *head)  //打印链表
 32 {
 33     printf("[%d] ", head->info);
 34     head = head->next;
 35 
 36     while (head != nullptr)
 37     {
 38         printf("%d ", head->info);
 39         head = head->next;
 40     }
 41     printf("
");
 42 }
 43 
 44 void createloop(pnode *head, int ind)   //给链表造环,ind为环入口下标(下标从1开始)
 45 {
 46     if (ind > head->info)
 47     {
 48         printf("入口节点下标不得大于链表节点数
");
 49         return;
 50     }
 51 
 52     pnode *entrance = head;
 53 
 54     while (ind--)
 55         entrance = entrance->next;
 56 
 57     while (head->next != nullptr)
 58         head = head->next;
 59 
 60     head->next = entrance;
 61 }
 62 
 63 void judge(pnode *head) //判断链表是否有环
 64 {
 65     pnode *slow, *fast;
 66 
 67     slow = head;
 68     fast = head->next;  //注意这里fast要先后移一位,有利于循环条件的编写
 69 
 70     while (fast!=nullptr && fast!=slow)
 71     {
 72         fast = fast->next->next;
 73         slow = slow->next;
 74     }
 75 
 76     if (fast == slow)
 77     {
 78         printf("链表有环
");
 79         return;
 80     }
 81 
 82     else
 83     {
 84         printf("链表无环
");
 85         return;
 86     }
 87 }
 88 
 89 void findEntrance(pnode *head)    //寻找链表环入口
 90 {
 91     pnode *slow, *fast;
 92 
 93     slow = head;
 94     fast = head->next;
 95 
 96     while (fast!=nullptr && fast!=slow)
 97     {
 98         fast = fast->next->next;
 99         slow = slow->next;
100     }
101 
102     int entrance = 0;
103     slow = head;
104     fast = fast->next;  //经用例推算fast得后移一位,否则陷入死循环
105 
106     while (fast != slow)
107     {
108         fast = fast->next;
109         slow = slow->next;
110         entrance++;
111     }
112 
113     printf("入口节点下标为%d(从1开始)
", entrance);
114 }
115 
116 int main()
117 {
118     pnode *head;
119 
120     create(&head);
121     add(head, 1);
122     add(head, 2);
123     add(head, 3);
124     add(head, 4);
125     add(head, 5);
126     add(head, 6);
127     add(head, 7);
128     add(head, 8);
129     show(head);
130     judge(head);
131     createloop(head, 4);
132     judge(head);
133     findEntrance(head);
134 
135     return 0;
136 }

 

以上是关于单链表是否有环及环入口点的主要内容,如果未能解决你的问题,请参考以下文章

判断一个单链表是否有环及环的连接点

判断一个单链表是否有环及环的连接点

判断单链表是否存在环及寻找环的入口点

线性表练习之Example048-判断一个单链表是否有环,如果有则找出环的入口点并返回,否则返回 NULL

(转)求单链表是否有环,环入口和环长

有环单链表,找环的入口。