-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisloop.c
133 lines (125 loc) · 2.44 KB
/
isloop.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
// program to check whether there's loop in a s.l.l
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} * root;
// creation of s.l.l
void append(int ar[], int n)
{
struct node *temp, *last, *p;
temp = (struct node *)malloc(sizeof(struct node));
temp->data = ar[0];
temp->next = NULL;
root = temp;
last = temp;
for (int i = 1; i < n; i++)
{
p = (struct node *)malloc(sizeof(struct node));
p->data = ar[i];
p->next = NULL;
last->next = p;
last = p;
}
}
// displaying s.l.l
void display()
{
struct node *p;
p = root;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
}
// calculating length of s.l.l
int length(struct node *f)
{
struct node *p;
p = f;
int count = 0;
while (p)
{
count++;
p = p->next;
}
return count;
}
// creating a loop in s.l.l
void createloop()
{
int ele;
struct node *p, *last;
p = root;
last = root;
printf("\nLook at the above created S.L.L!");
printf("\nEnter the element to which you want the rear element to point to: ");
scanf("%d", &ele);
while (last->next != NULL)
{
if (ele == last->data)
{
p = last;
}
last = last->next;
}
if (p == root)
{
printf("\nThe element is not found!");
}
else
{
last->next = p;
printf("\nThe loop is successfully created!");
}
}
// checking whether there's loop
int checkloop()
{
struct node *p, *q;
p = root;
q = root;
do
{
p = p->next;
q = q->next;
if (q != NULL)
{
q = q->next;
}
else
{
q = NULL;
}
} while (p && q && p != q);
return p == q ? 1 : 0;
}
// print result
void printout(int n)
{
if (n == 0)
{
printf("\nThere's no loop");
}
else if (n == 1)
{
printf("\nThere's loop");
}
}
int main()
{
int n = 5;
int ar[] = {1, 3, 5, 7, 9};
append(ar, n);
printf("The created S.L.L: ");
display();
// at first there'll be no loop, display "There's no loop"
printout(checkloop());
createloop();
// now a loop is created, display "There's loop"
printout(checkloop());
return 0;
}