-
Notifications
You must be signed in to change notification settings - Fork 0
/
displaycll.c
86 lines (80 loc) · 1.64 KB
/
displaycll.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
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} * root;
// creation of a cll;
// N:- in cll, you can't have NULL
void create(int ar[], int n)
{
struct node *last, *p;
root = (struct node *)malloc(sizeof(struct node));
root->data = ar[0];
root->next = root;
last = root;
for (int i = 1; i < n; i++)
{
p = (struct node *)malloc(sizeof(struct node));
p->data = ar[i];
p->next = last->next;
last->next = p;
last = p;
}
printf("\nThe c.l.l is successfully created!");
}
// displaying cll
void display(struct node *p)
{
do
{
printf("%d ", p->data);
p = p->next;
} while (p != root);
}
// displaying cll using recursion
void displayrec(struct node *p)
{
static int flag = 0;
if (p != root || flag == 0)
{
flag = 1;
printf("%d ", p->data);
displayrec(p->next);
}
flag = 0;
}
// checking for the 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;
}
int main()
{
int len = 5;
int a[] = {1, 2, 3, 4, 5};
create(a, len);
printf("\nThis should display a no. of loops present: %d", checkloop());
printf("\nThe c.l.l is: ");
display(root);
printf("\nThe c.l.l. using recursion: ");
displayrec(root);
return 0;
}