-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_linked_list.c
301 lines (253 loc) · 8.68 KB
/
circular_linked_list.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <stdio.h>
#include <stdlib.h>
/*
GOAL OF THIS CODE:
Create a fully functional circular linked list
1. there should be a function for adding a node in the list wherever the user wants.
2. there should be a function for deletion of a node in the list.
3. there should be a function for printing the circular linked list from the element given by the user.
4. there should be a function to find if a particular node is present in the list or not.
5. there should be a function to find out the number of nodes in the list.
6. there should be a function to find out if the list is empty or not.
7. there should be a function that changes the reference to the list.
*/
struct node {
int data;
struct node* next;
};
struct node* list_initializer(int node1_data, int node2_data, int node3_data, int node4_data)
{
struct node* node1 = (struct node*) malloc(sizeof(struct node));
struct node* node2 = (struct node*) malloc(sizeof(struct node));
struct node* node3 = (struct node*) malloc(sizeof(struct node));
struct node* node4 = (struct node*) malloc(sizeof(struct node));
if (node1 != NULL)
{
node1->data = node1_data;
node1->next = node2;
}
if (node2 != NULL)
{
node2->data = node2_data;
node2->next = node3;
}
if (node3 != NULL)
{
node3->data = node3_data;
node3->next = node4;
}
if (node4 != NULL)
{
node4->data = node4_data;
node4->next = node1;
}
return node1;
}
void node_adder(struct node** ref, int new_node_data, int node_finder_data)
{
struct node* ptr = *ref;
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = new_node_data;
do
{
if (ptr->data == node_finder_data)
{
new_node->next = ptr->next;
ptr->next = new_node;
}
ptr = ptr->next;
} while (ptr->next != (*ref)->next);
*ref = new_node;
}
void node_deleter(struct node** ref, int node_finder_data)
{
if (*ref != NULL)
{
struct node* ptr = *ref;
do
{
if (ptr->next->data == node_finder_data)
{
struct node* temp = ptr->next;
printf("node with data = \'%d\' deleted.\n", temp->data);
ptr->next = ptr->next->next;
*ref = ptr->next;
free(temp);
return;
}
ptr = ptr->next;
} while (ptr->next != (*ref)->next);
}
else
{
printf("list is empty. Nothing to delete.\n");
}
}
void list_printer(struct node* ref)
{
struct node* ptr = ref;
printf("ref -> ");
do
{
printf("%d -> ", ptr->data);
ptr = ptr->next;
} while (ptr->next != ref->next);
printf("ref\n");
}
void node_checker(struct node* ref, int node_finder_data)
{
struct node* ptr = ref;
int position = 1;
do
{
if (ptr->data == node_finder_data)
{
printf("node with data = \'%d\' found in list at %d position.\n", node_finder_data, position);
return;
}
ptr = ptr->next;
position = position + 1;
} while (ptr->next != ref->next);
printf("node with data = \'%d\' not found in the list.\n", node_finder_data);
}
int node_counter(struct node* ref)
{
struct node* ptr = ref;
int counter = 0;
do
{
counter = counter + 1;
ptr = ptr->next;
} while (ptr->next != ref->next);
// printf("total number of nodes found in the list = \'%d\'", counter);
return counter;
}
void list_empty_checker(struct node** ref)
{
if (*ref != NULL)
{
printf("list is not empty. at present there are %d nodes in the list\n", node_counter((*ref)));
}
else
{
printf("list is empty.\n");
}
}
void ref_changer(struct node** ref, int node_finder_data)
{
struct node* ptr = *ref;
do
{
if (ptr->data == node_finder_data)
{
printf("node with data = \'%d\' found. Reference point changed!\n", node_finder_data);
*ref = ptr;
return;
}
ptr = ptr->next;
} while (ptr->next != (*ref)->next);
printf("node with data = \'%d\' not found! reference change aborted.\n", node_finder_data);
}
int main()
{
struct node* ref = NULL;
int node1_data, node2_data, node3_data, node4_data;
int new_node_data;
int node_finder_data;
int mode;
int test_setup;
printf("this program depicts the implementation of a circular linked list.\n");
printf("following are the Options avaliable for list manipulation:\n1.Initialize List.\n2.Add a node in the list.\n3.delete a node from the list.\n4.check if a node is present in the list\n5.check the number of nodes present in the list\n6.check if the list is empty\n7.change reference in the list\n8.Print the circular linked list\n9.Re-print the manipulation panel.\n10.EXIT\n");
while(mode != 10)
{
printf("please enter the operation you want to perform\n");
scanf("%d", &mode);
switch (mode)
{
case 1:
printf("do you want to load the test setup?\n");
scanf("%d", &test_setup);
if (test_setup == 1)
{
printf("test setup loaded successfully\n");
node1_data = 10;
node2_data = 20;
node3_data = 30;
node4_data = 40;
}
else
{
printf("please enter the node data\n");
printf("node1 data:\n");
scanf("%d", &node1_data);
printf("node2 data:\n");
scanf("%d", &node2_data);
printf("node3 data:\n");
scanf("%d", &node3_data);
printf("node4 data:\n");
scanf("%d", &node4_data);
}
ref = list_initializer(node1_data, node2_data, node3_data, node4_data);
break;
case 2:
printf("\nThis will add a new node in the list.\n");
printf("please enter the data for the new node");
scanf("%d", &new_node_data);
printf("please enter the data of the node after which you would like to add the new node.\n");
scanf("%d", &node_finder_data);
node_adder(&ref, new_node_data, node_finder_data);
break;
case 3:
printf("\nThis will delete a node from the list\n");
printf("please enter the data of the node you would like to delete.\n");
scanf("%d", &node_finder_data);
node_deleter(&ref, node_finder_data);
break;
case 4:
printf("please enter the data of the node you would like to check for in the list.\n");
scanf("%d", &node_finder_data);
node_checker(ref, node_finder_data);
break;
case 5:
printf("total number of nodes found in the list = \'%d\'", node_counter(ref));
break;
case 6:
list_empty_checker(&ref);
break;
case 7:
printf("\nthis will change the reference point in the list.\n");
printf("please enter the data of the node you would like to make the new ref. point\n");
scanf("%d", &node_finder_data);
ref_changer(&ref, node_finder_data);
break;
case 8:
list_printer(ref);
break;
case 9:
printf("following are the Options avaliable for list manipulation:\n1.Re-Initialize List.\n2.Add a node in the list.\n3.delete a node from the list.\n4.check if a node is present in the list\n5.check the number of nodes present in the list\n6.check if the list is empty\n7.change reference in the list\n8.Print the circular linked list\n9.Re-print the manipulation panel.\n10.EXIT\n");
break;
case 10:
goto exit;
break;
default:
printf("operation not found!!! please check the operations list and try again.\n");
break;
}
}
exit:
return 0;
}
/*
**FOR TESTING ONLY**
ref = list_initializer(10, 20, 30, 40);
list_printer(ref);
node_adder(&ref, 555, 40);
list_printer(ref);
node_checker(ref, 41);
node_deleter(&ref, 555);
list_printer(ref);
node_counter(ref);
list_empty_checker(&ref);
ref_changer(&ref, 40);
list_printer(ref);
*/