-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.c
346 lines (275 loc) · 6.54 KB
/
lists.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
#include "lists.h"
struct _stack {
size_t size;
node *head;
};
struct _node {
item this;
node *next;
};
/* LIST */
list *create_list()
{
list *new_list = NULL;
/* Create List */
new_list = (list *)malloc(sizeof(list));
if(new_list == NULL)
memory_error("Unable to reserve list memory");
/* Set head to null */
new_list->head = NULL;
/* Set size to 0 :( */
new_list->size = 0;
return new_list;
}
node *get_head(list *got_list)
{
return got_list->head;
}
void set_head(list *got_list, node *got_head)
{
got_list->head = got_head;
return;
}
void push_item_to_list(list *got_list, item new_item)
{
/* Node Creation */
node *new_node = NULL;
new_node = create_node(new_item, NULL);
/* Prepend node to list*/
push_node_to_list(got_list, new_node);
return;
}
void push_node_to_list(list *got_list, node *got_node)
{
got_node->next = got_list->head;
got_list->head = got_node;
/* Increase List size :D */
got_list->size ++;
return;
}
size_t get_list_size(list *got_list)
{
return got_list->size;
}
void print_list(list *got_list, void (*print_item)(item))
{
node *aux_node;
printf("Print list:\n");
fprintf(DEBUG_LOC, "Size of list: %lu\n", get_list_size(got_list));
for(aux_node = get_head(got_list);
aux_node != NULL;
aux_node = get_next_node(aux_node))
{
printf("[ ");
print_item(get_node_item(aux_node));
printf("] -> \n");
}
printf("-> [ " KBLU "NULL" RESET " ]");
}
void free_list(list *got_list, void (*free_item)(item))
{
node *got_node = get_head(got_list);
/* Free every node of list*/
free_connected_nodes(got_node, free_item);
/* Bring freedom to stack */
free(got_list);
return;
}
list *sort_list (list *to_sort, int (*cmp_list)(item, item))
{
int num_nodes = get_list_size(to_sort);
int counter;
node *current;
node *next;
node *previous;
for (counter = 0; counter < num_nodes; counter++) {
current = get_head(to_sort);
next = get_next_node(current);
previous = NULL;
while(next != NULL) {
int compare = cmp_list(get_node_item(current), get_node_item(next));
if (compare == 0) {
if (current == get_head(to_sort)){
to_sort->head = next;
} else {
previous->next = next;
}
current->next = next->next;
next->next = current;
previous = next;
next = current->next;
}
else {
previous = current;
current = current->next;
next = current->next;
}
}
}
return to_sort;
}
/* STACK */
stack *create_stack()
{
stack *new_stack = NULL;
new_stack = (stack *)malloc(sizeof(stack));
if(new_stack == NULL)
memory_error("Unable to reserve stack memory");
new_stack->size = 0;
new_stack->head = NULL;
return new_stack;
}
void push_to_stack(stack *got_stack, item new_item)
{
/* Node Creation */
node *new_node = NULL;
new_node = create_node(new_item, got_stack->head);
/* Increase size of Stack */
got_stack->size++;
got_stack->head = new_node;
return;
}
node *pop_from_stack(stack *got_stack)
{
node *got_node;
/* Get stack head*/
got_node = got_stack->head;
if(got_node != NULL)
{
/* Change stack head to next node*/
got_stack->head = got_node->next;
got_stack->size--;
}
/* Reduce stack size */
return got_node;
}
node *get_stack_head(stack *got_stack)
{
return got_stack->head;
}
size_t get_stack_size(stack *got_stack)
{
return got_stack->size;
}
int is_stack_empty(stack *got_stack)
{
if(got_stack->head == NULL)
{
return TRUE;
}
else
{
return FALSE;
}
}
void print_stack(stack *got_stack, void (*print_item)(item))
{
node *aux_node;
/* Print stack size to selected DEBUG_LOC*/
fprintf(DEBUG_LOC, "Size of stack: %lu\n", get_stack_size(got_stack));
/* For each node print content*/
for(aux_node = got_stack->head;
aux_node != NULL;
aux_node = get_next_node(aux_node))
{
print_item(get_node_item(aux_node));
}
}
void free_stack(stack *got_stack, void (*free_item)(item))
{
node *got_node = NULL;
/* Free every node of stack*/
for(got_node = pop_from_stack(got_stack);
got_node != NULL;
got_node = pop_from_stack(got_stack))
{
free_node(got_node, free_item);
}
/* Bring America to stack */
free(got_stack);
return;
}
/* NODE */
node *create_node(item new_item, node *next_node)
{
node *new_node = NULL;
/* Memory allocation */
new_node = (node *)malloc(sizeof(node));
if (new_node == NULL)
memory_error("Unable to reserve node memory");
/* Add item to node*/
new_node->this = new_item;
new_node->next = next_node;
return new_node;
}
item get_node_item(node *got_node)
{
return got_node->this;
}
node *get_next_node(node *got_node)
{
return got_node->next;
}
void free_node(node *got_node, void (*free_item)(item))
{
/* Free node item */
free_item(got_node->this);
/* Free the node to save on the load */
free(got_node);
return;
}
void free_connected_nodes(node *got_node, void (*free_item)(item))
{
node *aux_node = NULL;
/* Free every node connected*/
while(got_node != NULL)
{
aux_node = get_next_node(got_node);
free_node(got_node, free_item);
got_node = aux_node;
}
return;
}
void merge_lists(list *list_a, list *list_b)
{
node *aux_node;
node *final_head;
size_t final_size;
final_size = list_a->size + list_b->size;
if(list_a->head == NULL)
{
list_a->head = list_b->head;
list_b->head = NULL;
return;
}
else if(list_b->head == NULL)
{
return;
}
if(list_a->size < list_b->size)
{
aux_node = list_a->head;
final_head = list_b->head;
list_a->size = final_size;
}
else
{
aux_node = list_b->head;
final_head = list_a->head;
list_b->size = final_size;
list_a->head = list_b->head;
}
while(aux_node->next != NULL)
{
aux_node = get_next_node(aux_node);
}
list_b->head = NULL;
aux_node->next = final_head;
return;
}
void already_free(item got_item)
{
if( got_item != NULL )
return;
return;
}