forked from vivekanand44/codes-Youtube-videos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect loop in a linked list.cpp
70 lines (68 loc) · 1.51 KB
/
detect loop in a linked list.cpp
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
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *newNode(int data)
{
node *p;
p = (node*)malloc(sizeof(node));
p->data = data; // First node
p->next = NULL;
return p;
}
int main()
{
int len_loop;
node *p , *q , *loop_node , *one_step , *two_steps , *length_calculator;
p = newNode(1);
p->next = newNode(2);
p->next->next = newNode(3);
p->next->next->next = newNode(4);
loop_node = p->next->next->next;
p->next->next->next->next = newNode(5);
p->next->next->next->next->next = newNode(6);
p->next->next->next->next->next->next = newNode(7);
p->next->next->next->next->next->next->next = newNode(8);
p->next->next->next->next->next->next->next->next = loop_node;
//detect loop code
one_step = p;
two_steps = p;
while(1)
{
if(one_step == NULL || two_steps == NULL)
{
printf("\n There is no loop");
break;
}
one_step = one_step->next;
two_steps = two_steps->next->next;
if(one_step == two_steps)
{
printf("\n There is a loop");
break;
}
}
//find the length of the loop
len_loop = 1;
length_calculator = one_step->next;
while(length_calculator!=one_step)
{
len_loop = len_loop+1;
length_calculator = length_calculator->next;
}
printf("\n\n Length of the loop is %d",len_loop);
//find the starting point of the loop
// p is at the starting of the linked list
q = one_step;
while(p!=q)
{
p = p->next;
q = q->next;
}
printf("\n The starting point of the loop is %d", p->data);
return 0;
}