forked from hariom20singh/data-structure-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloydAlgo.cpp
More file actions
92 lines (92 loc) · 1.47 KB
/
FloydAlgo.cpp
File metadata and controls
92 lines (92 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <bits/stdc++.h>
using namespace std;
// linked list
class node
{
public:
int data;
node *next;
node(int k)
{
data = k;
next = NULL;
}
};
void IAT(node *&head, int p) //Inserting data at the tail of the linked list
{
node *n = new node(p);
if (head == NULL)
{
head = n;
return;
}
node *t = head;
while (t->next != NULL)
{
t = t->next;
}
t->next = n;
}
bool floyds(node *&head) // function to detect the cycle
{
node *fast = head;
node *slow = head;
while (fast != NULL && fast->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
if (fast == slow)
{
return true;
}
}
return false;
}
void floyd(node *&head) // function print the start of the cycle
{
if (floyds(head) == 1)
{
node *slow = head;
node *fast = head;
do
{
fast = fast->next->next;
slow = slow->next;
} while (fast != slow);
fast = head;
while (slow->next != fast->next)
{
fast = fast->next;
slow = slow->next;
}
cout<<slow->next->data<<"\n";
}
else
{
cout<<"There is no cycle in the linked list\n";
return;
}
}
int main(){
// making a linear linked list
node *head = NULL;
IAT(head, 1);
IAT(head, 2);
IAT(head, 3);
IAT(head, 4);
IAT(head, 5);
IAT(head, 6);
IAT(head, 7);
floyd(head); // function call
node*temp = head;
node*cycle = NULL;
while(temp->next!=NULL){
if(temp->data == 4){
cycle = temp;
}
temp = temp->next;
}
temp->next = cycle; // making a cycle in linked list
floyd(head); // function call
return 0;
}