forked from nishitpanchal395/projecthactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_loop.cpp
More file actions
73 lines (70 loc) · 1.15 KB
/
detect_loop.cpp
File metadata and controls
73 lines (70 loc) · 1.15 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
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *link;
int flag;
};
node *head = NULL;
void insert(int d)
{
node *ptr=new node();
ptr->data=d;
ptr->flag=0;
ptr->link=NULL;
if(head==NULL)
head=ptr;
else{
node*temp =head;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=ptr;
}
}
void display()
{
node *temp = head;
while (temp != NULL)
{
cout << temp->data << "->";
temp = temp->link;
}
cout << "\n";
}
bool deletect_loop(node *h)
{
while (h != NULL)
{
if (h->flag == 1)
return true;
h->flag = 1;
h = h->link;
}
return false;
}
int main()
{
insert(1);
display();
cout << endl;
insert(2);
display();
cout << endl;
insert(3);
display();
cout << endl;
insert(4);
display();
cout << endl;
cout << endl;
head->link->link->link->link=head;
if(deletect_loop(head))
cout<< "true";
else
cout<<"false";
return 0;
}