-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop.cpp
84 lines (75 loc) · 1.42 KB
/
loop.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//loop applied successfully......
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
node(int data){
this->data=data;
this->next=NULL;
}
};
class linked_list{
node * head;
node * start;
public:
linked_list(){
head=NULL;
start=NULL;
}
void insertion(int item){
if(head==NULL){
head=new node(item);
start=head;
return;
}
node *np = new node(item);
start->next=np;
start=np;
}
void makeloop(){
if(head!=NULL){
start->next=start;
}
}
void checkloop(){
int flag=0;
node * ptr1 = head;
node * ptr2 = head;
while(ptr2 != NULL && ptr2->next != NULL){
ptr1=ptr1->next;
ptr2=ptr2->next->next;
if(ptr1==ptr2){
flag=1;
cout<<"\nloop exists.";
break;
}
}
if(flag==0)
cout<<"\ndoes not exists.";
return;
}
void display(){
node *temp=head;
if(temp==NULL){
cout<<"\nlist is empty ....";
return;
}
while(temp!=NULL){
cout<<temp->data<<"\t";
temp=temp->next;
}
return;
}
};
int main(){
linked_list l1;
l1.insertion(9);
l1.insertion(2);
l1.insertion(3);
l1.insertion(4);
l1.insertion(7);
l1.makeloop();
l1.checkloop();
return 0;
}