-
Notifications
You must be signed in to change notification settings - Fork 0
/
clist.c
280 lines (255 loc) · 6.4 KB
/
clist.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
#include <stdio.h>
#include<stdlib.h>
#include<limits.h>
#include <stdbool.h>
#include "clist.h"
// Function to initialize the circular linked list
void init_CLL(CLL *list) {
*list = NULL;
}
// Function to insert an element at the beginning
void insert_beg(CLL *list, int data) {
node *new_node = (node *)malloc(sizeof(node));
if(new_node)
new_node->data = data;
else
return;
if (*list == NULL) {
new_node->next = new_node;
*list = new_node;
} else {
node *temp = *list;
while (temp->next != *list) {
temp = temp->next;
}
new_node->next = *list;
temp->next = new_node;
*list = new_node;
}
}
// Function to insert an element at the end
void insert_end(CLL *list, int data) {
node *new_node = (node *)malloc(sizeof(node));
if(new_node)
new_node->data = data;
else
return;
if (*list == NULL) {
new_node->next = new_node;
*list = new_node;
} else {
node *temp = *list;
while (temp->next != *list) {
temp = temp->next;
}
temp->next = new_node;
new_node->next = *list;
}
}
// Function to insert an element at a specific position
void insert_pos(CLL *list, int data, int pos) {
if (pos < 1) {
printf("Invalid position.\n");
return;
}
node *new_node = (node *)malloc(sizeof(node));
if(new_node)
new_node->data = data;
else
return;
if (*list == NULL) {
if (pos == 1) {
new_node->next = new_node;
*list = new_node;
} else {
printf("Position out of bounds.\n");
}
return;
}
if (pos == 1) {
insert_beg(list, data);
return;
}
node *temp = *list;
int count = 1;
while (count < pos - 1 && temp->next != *list) {
temp = temp->next;
count++;
}
if (count == pos - 1) {
new_node->next = temp->next;
temp->next = new_node;
} else {
printf("Position out of bounds.\n");
free(new_node);
}
}
// Function to remove the element at the beginning
void remove_beg(CLL *list) {
if (*list == NULL) {
printf("List is empty.\n");
return;
}
if (*list == (*list)->next) {
free(*list);
*list = NULL;
} else {
node *temp = *list;
while (temp->next != *list) {
temp = temp->next;
}
node *to_delete = *list;
temp->next = (*list)->next;
*list = (*list)->next;
free(to_delete);
}
}
// Function to remove the element at the end
void remove_end(CLL *list) {
if (*list == NULL) {
printf("List is empty.\n");
return;
}
if (*list == (*list)->next) {
free(*list);
*list = NULL;
} else {
node *temp = *list;
node *prev = NULL;
while (temp->next != *list) {
prev = temp;
temp = temp->next;
}
prev->next = *list;
free(temp);
}
}
// Function to remove an element at a specific position
void remove_pos(CLL *list, int pos) {
if (*list == NULL) {
printf("List is empty.\n");
return;
}
if (pos < 1) {
printf("Invalid position.\n");
return;
}
if (pos == 1) {
remove_beg(list);
return;
}
node *temp = *list;
node *prev = NULL;
int count = 1;
while (count < pos && temp->next != *list) {
prev = temp;
temp = temp->next;
count++;
}
if (count == pos) {
if (temp->next == *list) {
prev->next = *list;
} else {
prev->next = temp->next;
}
free(temp);
} else {
printf("Position out of bounds.\n");
}
}
// Function to sort the elements in the CLL
void sort(CLL *list) {
if (*list == NULL) {
printf("List is empty.\n");
return;
}
node *i, *j;
int temp;
for (i = *list; i->next != *list; i = i->next) {
for (j = i->next; j != *list; j = j->next) {
if (i->data > j->data) {
temp = i->data;
i->data = j->data;
j->data = temp;
}
}
}
}
// Function to display the elements in the CLL
void display(CLL list) {
if (list == NULL) {
printf("List is empty.\n");
return;
}
node *temp = list;
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != list);
printf("\n");
}
//TO display after enter n as next in circular way
void display_bystep(CLL list){
char *c;
if (list == NULL) {
printf("List is empty.\n");
return;
}
node *temp = list;
c = (char*)malloc(sizeof(char));
while (temp != NULL) {
scanf("%c", c);
if(*c == 'e')
break;
printf("%d \n", temp->data);
temp = temp->next;
}
free(c);
printf("\n");
}
//count total nodes of CLL
int totalnodes(CLL list) {
if (list == NULL) {
return 0;
}
int count = 1;
node *temp = list;
while (temp->next != list) {
count++;
temp = temp->next;
}
return count;
}
// Function to reverse the circular linked list
void reverse(CLL *list) {
if (*list == NULL || (*list)->next == *list) {
return; // List is empty or has only one node
}
node *prev = NULL, *current = *list, *next = NULL;
//node *last = *list;
// Traverse the list and reverse the links
do {
next = current->next;
current->next = prev;
prev = current;
current = next;
} while (current != *list);
// Fix the last node and the head
(*list)->next = prev;
*list = prev;
}
// Function to free all nodes in the circular linked list
void free_list(CLL *list) {
if (*list == NULL) {
return;
}
node *current = *list, *next;
// Make the last node's next pointer NULL to stop looping
while (current->next != *list) {
next = current->next;
free(current);
current = next;
}
free(current); // Free the last node
*list = NULL; // Set list to NULL after freeing
}