forked from VanNevlin/KumpulAssignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.cpp
47 lines (39 loc) · 765 Bytes
/
2.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
#include <stdio.h>
#include <stdlib.h>
struct Node{
int n;
int flag;
Node *next;
}*head, *tail;
Node *createNode(int n) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->n = n;
newNode->next = NULL;
return newNode;
}
void pushHead(int n, Node** curr) {
Node *temp = createNode(n);
temp->next = *curr;
*curr = temp;
}
bool findLoop(Node* f){
while(f != NULL){
if(f->flag == 1) return true;
else{
f->flag = 1;
f = f->next;
}
}
return false;
}
int main(){
Node* ans = NULL;
pushHead(20, &ans);
pushHead(15, &ans);
pushHead(10, &ans);
pushHead(5, &ans);
ans->next->next->next->next = ans;
if(findLoop(ans)) printf("Loop Detected\n");
else printf("Loop Not Found\n");
return 0;
}