-
Notifications
You must be signed in to change notification settings - Fork 0
/
unrolled_list.c
378 lines (294 loc) · 10.8 KB
/
unrolled_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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
typedef struct Node {
size_t elem_count;
struct Node *next;
int *data;
} Node;
typedef struct {
size_t len;
size_t node_capacity;
size_t threshold;
struct Node *head;
struct Node *tail;
} List;
List* list_init(size_t capacity) {
List *list = malloc(sizeof(List));
if (!list) {
fprintf(stderr, "error: memory allocation failed!\n");
exit(-1);
}
list->node_capacity = capacity;
list->threshold = (capacity/2) + 1;
list->head = NULL;
list->tail = NULL;
list->len = 0;
return list;
}
void list_clear(List *self) {
if (self->len <= 0)
return;
Node *current = self->head;
Node *next;
while (current != NULL) {
next = current->next;
free(current->data);
free(current);
current = next;
}
self->head = NULL;
self->tail = NULL;
self->len = 0;
}
void list_deinit(List **self) {
list_clear(*self); // Deallocate the list nodes
free(*self); // Deallocate the list structure
*self = NULL; // Remove the pointer reference to the list
}
Node* new_node(List *self, int val, Node *next) {
Node *node = malloc(sizeof(Node));
if (!node) {
fprintf(stderr, "error: memory allocation failed!\n");
exit(-1);
}
node->data = (int*)malloc(self->node_capacity * sizeof(int));
if (!(node->data)) {
fprintf(stderr, "error: memory allocation failed!\n");
free(node);
exit(-1);
}
node->data[0] = val;
node->next = next;
node->elem_count = 0;
return node;
}
void shift_right(int *arr, size_t size, size_t src, size_t desc) {
if (src >= desc || desc > size || src < 0) {
return;
}
for (size_t i = desc; i > src; i--)
arr[i] = arr[i - 1];
}
void shift_left(int *arr, size_t size, size_t src, size_t desc) {
if (desc <= src || src > size || src < 0) {
return;
}
for (size_t i = src; i < desc; i++)
arr[i] = arr[i + 1];
}
int list_is_empty(const List *self) {
return (self->len <= 0);
}
size_t list_len(const List *self) {
return self->len;
}
// Adds an element to the head of the list.
void list_add(List *self, int val) {
if (self->len <= 0) {
self->head = new_node(self, val, NULL);
self->tail = self->head;
} else if (self->head->elem_count < self->node_capacity) {
shift_right(self->head->data, self->node_capacity, 0, self->head->elem_count);
self->head->data[0] = val;
} else {
Node *fresh_node = new_node(self, val, self->head);
size_t idx;
while (fresh_node->elem_count != self->threshold) {
idx = (self->head->elem_count - self->threshold) + fresh_node->elem_count;
fresh_node->data[++fresh_node->elem_count] = self->head->data[idx];
}
self->head->elem_count -= self->threshold;
self->head = fresh_node;
}
self->len++;
self->head->elem_count++;
}
// Appends an element at the end of the list.
void list_append(List *self, int val) {
if (self->len <= 0) {
self->head = new_node(self, val, NULL);
self->tail = self->head;
} else if (self->tail->elem_count < self->node_capacity) {
self->tail->data[self->tail->elem_count] = val;
} else {
Node *fresh_node = new_node(self, 0, NULL);
fresh_node->data[self->threshold] = val;
size_t idx;
while (fresh_node->elem_count != self->threshold) {
idx = (self->tail->elem_count - self->threshold) + fresh_node->elem_count;
fresh_node->data[fresh_node->elem_count++] = self->tail->data[idx];
}
self->tail->elem_count -= self->threshold;
self->tail->next = fresh_node;
self->tail = fresh_node;
}
self->len++;
self->tail->elem_count++;
}
// Inserts an element at the specified position in the list.
// If the position is greater than the length of the list or less than 0,
// the element is either added to the head (if pos < 0) or appended to the end (if pos > length).
void list_insert(List *self, int val, size_t pos) {
if (pos > self->len || pos < 0 || self->len <= 0) {
return (pos < 0 ? list_add(self, val) : list_append(self, val));
}
Node *curr = self->head;
while (pos > curr->elem_count) {
pos -= curr->elem_count;
curr = curr->next;
}
if (curr->elem_count < self->node_capacity) {
shift_right(curr->data, self->node_capacity, pos, curr->elem_count);
curr->data[pos] = val;
curr->elem_count++;
} else {
int temp_val = curr->data[curr->elem_count - 1];
Node *fresh_node = new_node(self, 0, curr->next);
curr->next = fresh_node;
shift_right(curr->data, self->node_capacity, pos, curr->elem_count);
curr->data[pos] = val;
size_t idx;
while (fresh_node->elem_count != self->threshold) {
idx = (curr->elem_count - self->threshold) + fresh_node->elem_count;
fresh_node->data[fresh_node->elem_count++] = self->tail->data[idx];
}
curr->elem_count -= self->threshold;
fresh_node->data[fresh_node->elem_count++] = temp_val;
if (curr == self->tail)
self->tail = fresh_node;
}
self->len++;
}
// Removes and returns the first element from the list.
int list_chop(List *self) {
if (self->len <= 0) {
fprintf(stderr, "error: fail to chop, list is empty!\n");
return -1;
}
int rem_elem = self->head->data[0];
self->len--;
if (self->head->elem_count <= 1) {
Node *temp = self->head;
self->head = self->head->next;
free(temp);
} else {
shift_left(self->head->data, self->node_capacity, 0, self->head->elem_count);
self->head->elem_count--;
}
return rem_elem;
}
// Removes the first occurrence of the specified value from the list.
int list_remove(List *self, int val) {
if (self->len <= 0) {
fprintf(stderr, "error: fail to remove, list is empty!\n");
return -1;
}
Node *curr = self->head->next;
Node *prev = self->head;
int rem_elem = -1;
size_t idx = 0;
while (curr != NULL) {
for ( ; idx < curr->elem_count; idx++) {
if (curr->data[idx] == val) {
shift_left(curr->data, self->node_capacity, idx, curr->elem_count);
curr->elem_count--;
rem_elem = val;
break;
}
}
prev = curr;
curr = curr->next;
}
if (rem_elem <= -1) {
return -1;
}
self->len--;
if (curr && curr->elem_count <= 0) {
prev->next = curr->next;
free(curr);
}
return rem_elem;
}
// Removes and returns the last element from the list.
int list_pop(List *self) {
if (self->len <= 0) {
fprintf(stderr, "error: fail to pop, list is empty!\n");
return -1;
}
int rem_elem = self->tail->data[self->tail->elem_count - 1];
self->len--;
if (self->tail->elem_count > 1) {
self->tail->elem_count--;
} else {
Node *temp = self->head;
while (temp->next->next != NULL)
temp = temp->next;
self->tail = temp;
temp = temp->next;
self->tail->next = NULL;
free(temp);
}
return rem_elem;
}
void list_display(const List *self) {
if (self->len <= 0) {
fprintf(stderr, "list is empty!\n");
return;
}
Node *temp = self->head;
while (temp != 0) {
for (int i = 0; i < temp->elem_count; i++)
printf("%d->", temp->data[i]);
temp = temp->next;
}
printf("STOP\n");
}
int main() {
// Initialize a circular list
List *list = list_init(6);
// Test adding elements to the head
list_add(list, 1);
list_add(list, 2);
list_add(list, 3);
// Display the list: Expected output: 3->2->1->
list_display(list);
// Test appending elements at the end
list_append(list, 4);
list_append(list, 5);
list_append(list, 6);
// Display the list: Expected output: 3->2->1->4->5->6->
list_display(list);
// Test inserting an element at a specific position
list_insert(list, 10, 2);
list_insert(list, 20, 5);
// Display the list: Expected output: 3->2->10->1->4->20->5->6->
list_display(list);
// Test removing an element from the head
int removed = list_chop(list);
printf("Removed element: %d\n", removed); // Expected output: 3
// Display the list: Expected output: 2->10->1->4->20->5->6->
list_display(list);
// Test removing the first occurrence of a specific value
int removed_value = list_remove(list, 4);
printf("Removed element: %d\n", removed_value); // Expected output: 4
// Display the list: Expected output: 2->10->1->20->5->6->
list_display(list);
// Test removing an element from the end
int popped = list_pop(list);
printf("Popped element: %d\n", popped); // Expected output: 6
// Display the list: Expected output: 2->10->1->20->5->
list_display(list);
// Clear the list
list_clear(list);
// Display the list after clearing: Expected output: list is empty!
list_display(list);
// Test inserting elements after clearing
list_insert(list, 100, 0);
list_insert(list, 200, 1);
// Display the list: Expected output: 200->100->
list_display(list);
// Deallocate memory and deinitialize the list
list_deinit(&list);
return 0;
}