-
Notifications
You must be signed in to change notification settings - Fork 0
/
DetectCircullar.c
155 lines (144 loc) · 2.64 KB
/
DetectCircullar.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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct Node {int x; struct Node *next;} Node;
int insert(Node **start, int x)
{
Node *p = *start;
if(*start == NULL)
{
*start = (Node *)malloc(sizeof(Node));
if(*start == NULL)
return 0;
(*start)->x = x;
(*start)->next = NULL;
}
else
{
while(p->next != NULL)
p = p->next;
p->next = (Node *)malloc(sizeof(Node));
if(p->next == NULL)
return 0;
p->next->x = x;
p->next->next = NULL;
}
}
int delete(Node **start, int *x)
{
Node *p = *start;
if(*start == NULL)
return 0;
else if((*start)->next == NULL)
{
*x = (*start)->x;
free(*start);
*start = NULL;
return 1;
}
else
{
while(p->next->next != NULL)
p = p->next;
*x = p->next->x;
free(p->next);
p->next = NULL;
return 1;
}
}
void display(Node *start)
{
while(start != NULL)
{
printf("%d -> ", start->x);
start = start->next;
}
printf("NULL\n");
}
int makeNewLink(Node *start)
{
Node *p = start, *q;
char c = '\0';
printf("\nEnter 'Y' to Point to that Node : ");
while(p != NULL)
{
printf("\nNode : %d", p->x);
printf("\nEnter choice = ");
fflush(stdin);
scanf("%c", &c);
if(c == 'Y' || c == 'y')
{
q = p;
break;
}
p = p->next;
}
while(p != NULL && p->next != NULL)
p = p->next;
if(p != NULL)
{
p->next = q;
return 1;
}
else
return 0;
}
void check(Node *start)
{
Node *q, *k; int cQ = 0, cK = 0;
if(start == NULL)
printf("List Empty");
else
{
for(q = start; q != NULL; q = q->next, cQ++)
{
for(k = start, cK = 0; k != q; k = k->next, cK++);
if(cK < cQ)
{
printf("Circullar Link Detected!");
return;
}
}
printf("List is Perfectly Linear");
}
}
int main()
{
Node *start = NULL;
int x, choice;
printf("\nEnter 1 to Insert");
printf("\nEnter 2 to Delete");
printf("\nEnter 3 to Display");
printf("\nEnter 4 to Create Circullar Link");
printf("\nEnter 5 to check");
printf("\nEnter 6 to Quit");
do
{
printf("\nEnter your choice = ");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter a No. = ");
scanf("%d", &x);
if(!insert(&start, x))
printf("Insertion Failed");
break;
case 2: if(!delete(&start, &x))
printf("Deletion Failed");
printf("\nDeleted = %d", x);
break;
case 3: display(start);
break;
case 4: if(!makeNewLink(start))
printf("Link Not made");
else
printf("Link Created");
break;
case 5: check(start);
break;
case 6: break;
}
}
while(choice != 6);
return 1;
}