-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion8.c
223 lines (176 loc) · 4.36 KB
/
question8.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
Given a single linked list. Check if it has a loop
Cannot be done by brute force (that is by checking for first node and then second and then third whether it is
repeating or not by traversing once for each node. There is a chance that it may enter an infinite loop
because of a loop ahead in the linked list)
METHOD1:
we can use a hash table and store addresses in by node number as we visit each node. If an address is encountered
again that means there is a loop
Time complexity: O(n)
Space complexity: O(1)
METHOD2:
In the design of the structure itself a new field can be introduced called visited. Whenever you visit
a node make that field one. It will be initialized to zero by default.
If you visit it again it will already be one, that means linked list has a loop
Time complexity: O(n)
Space complexity: O(n) //for new field n extra bits will be used
There is another possibility that we make a normal structure and later we make a new structure with that field
and copy all that data which may take up a lot of space. (so not recommended)
METHOD3:
We can have two pointers. One fast pointer can move ahead with two nodes each time and slow pointer with one node.
If there is a loop these two pointers will definately meet at a node.
It can be proved that this will happen in less than or equal to traversal of n nodes by slow node
Time complexity: O(n)
Space complexity: O(1)
*/
//METHOD1
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *link;
};
void printList(struct node *t){
if(t){
//interchanging these lines will print it in reverse order
printf("%d\n", t->data);
printList(t->link);
}
}
int checkLoop(struct node *head){
int hash[10];
int counter = 1;
struct node *t = head;
while(t->link){
if(hash[t->link-head]==t->link-head){
return 1;
}else{
hash[t->link-head] = t->link-head;
}
t = t->link;
}
return 0;
}
int main(){
struct node *head = (struct node *)malloc(sizeof(struct node));
struct node *t = head;
int counter = 1;
while(counter<=5){
t->data = counter*10;
if(counter == 5){
t->link = head;
}else{
t->link = (struct node *)malloc(sizeof(struct node));
}
t = t->link;
counter++;
}
t = head;
// printList(t);
//detecting if this has a loop
if(checkLoop(head)){
printf("there is a loop\n");
}else{
printf("there is no loop\n");
}
}
//================================================================================
//METHOD2
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
int visited;
struct node *link;
};
void printList(struct node *t){
if(t){
//interchanging these lines will print it in reverse order
printf("%d\n", t->data);
printList(t->link);
}
}
int checkLoop(struct node *head){
struct node *t = head;
while(t){
printf("visited value is %d\n", t->visited);
if(!t->visited){
t->visited = 1;
}else{
return 1;
}
t = t->link;
}
return 0;
}
int main(){
struct node *head = (struct node *)malloc(sizeof(struct node));
struct node *t = head;
int counter = 1;
while(counter<=5){
t->data = counter*10;
t->visited = 0;
if(counter == 5){
t->link = NULL;
}else{
t->link = (struct node *)malloc(sizeof(struct node));
}
t = t->link;
counter++;
}
t = head;
// printList(t);
//detecting if this has a loop
if(checkLoop(head)){
printf("there is a loop\n");
}else{
printf("there is no loop\n");
}
}
//==============================================================================================
//METHOD3
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *link;
};
void printList(struct node *t){
if(t){
//interchanging these lines will print it in reverse order
printf("%d\n", t->data);
printList(t->link);
}
}
int checkLoop(struct node *head){
struct node *slow,*fast = head;
for(slow=head,fast=head; fast && slow && fast->link;){
fast = fast->link->link;
slow = slow->link;
if(slow==fast){ return 1;}
}
return 0;
}
int main(){
struct node *head = (struct node *)malloc(sizeof(struct node));
struct node *t = head;
int counter = 1;
while(counter<=5){
t->data = counter*10;
if(counter == 5){
t->link = NULL;
}else{
t->link = (struct node *)malloc(sizeof(struct node));
}
t = t->link;
counter++;
}
t = head;
// printList(t);
//detecting if this has a loop
if(checkLoop(head)){
printf("there is a loop\n");
}else{
printf("there is no loop\n");
}
}