-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructures.c
427 lines (267 loc) · 8.46 KB
/
structures.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include "structures.h"
#include <stdlib.h>
//MISC.
char* val(Node* node_ptr){
return node_ptr->value;
}
Node* next(Node* node_ptr){
return node_ptr->next;
}
char strDif(char* s, char* t){
int i = 0;
while(*(s+i)==*(t+i)){
if(*(s+i)=='\0'){
return 0;
}
i++;
}
return 1;
}
//INSTANTIATION FUNCTIONS
Node* newNode(char* node_value){
Node* created_node = (Node*) malloc(sizeof(Node));
if(!created_node) return ERROR;
created_node -> value = node_value;
created_node -> next = NULLPTR;
return created_node;
}
List* newList(){
List* created_list = (List*)malloc(sizeof(List));
if(!created_list) return ERROR;
created_list->head = NULLPTR;
return created_list;
}
Queue* newQueue(){
Queue* created_queue = (Queue*)malloc(sizeof(Queue));
if(!created_queue) return ERROR;
created_queue->elements->head = NULLPTR;
}
Stack* newStack(){
Stack* created_stack = (Stack*)malloc(sizeof(Stack));
if(!created_stack) return ERROR;
created_stack->elements->head = NULLPTR;
return created_stack;
}
BST* newBST(int root_value){
BST* created_bst = (BST*)malloc(sizeof(BST));
if(!created_bst) return ERROR;
created_bst->father = NULLPTR;
created_bst->left_child = NULLPTR;
created_bst->right_child = NULLPTR;
created_bst->value = root_value;
return created_bst;
}
//LIST FUNCTIONS
Node* deleteNode(List* list, Node* node_ptr, int free_trigger){
int head_flag = 0;
Node* current_node = list->head;
if(current_node==node_ptr) head_flag++;
while(current_node->next!=node_ptr && !head_flag){
if(!current_node->next) return NULLPTR;
//If there is no such node the function returns an exception.
current_node = current_node->next;
}
if(head_flag) list->head = current_node->next;
else current_node->next = current_node->next->next;
if(free_trigger){
free(node_ptr);
return NULLPTR;
}
return node_ptr;
}
Node* deleteVal(List* list, char* value, int clear_buffer){
return deleteNode(list, searchNode(list, value), clear_buffer);
}
Node* deletePosition(List* list, int position, int clear_buffer){
return deleteNode(list, local(list, position), clear_buffer);
}
List* clearList(List* reference, int clear_buffer, int clear_elements_buffer){
Node*current = reference->head;
if(clear_elements_buffer){
while(current->next){
deleteNode(reference, current, 1);
current = current -> next;
}
deleteNode(reference, current, 1);
}
if(clear_buffer) {
free(reference);
return NULLPTR;
}
reference->head = NULLPTR;
return reference;
}
Node* first(List* list){
return list->head;
}
Node* last(List* list){
Node * current_node = list -> head;
if(!current_node) return NULLPTR;
while(current_node->next){
current_node = current_node -> next;
}
return current_node;
}
Node* searchNode(List* list, char* val){
Node* current_node = list->head;
while(current_node){
if(!strDif(current_node->value, val)) return current_node;
current_node = current_node->next;
}
return NULLPTR;
}
Node* local(List* list, int position){
int increment = 0;
Node* current_node = list->head;
while(increment < position){
if(!current_node->next) return NULLPTR;
current_node = current_node -> next;
increment++;
}
return current_node;
}
char append(List* list, Node* new_node){
Node * ptr_to_end = last(list);
if(!ptr_to_end) list->head = new_node;
else ptr_to_end -> next = new_node;
return SUCCESS;
}
char unshift(List* list, Node* new_node){
new_node -> next = list -> head;
list -> head = new_node;
return SUCCESS;
}
char insert(List* list, Node* new_node, int position){
Node * current_node = list -> head;
for(int increment = 0; increment < position; increment++){
if(!current_node -> next){
current_node -> next = new_node;
return SUCCESS;
}
current_node = current_node -> next;
}
new_node -> next = current_node -> next;
current_node -> next = new_node;
return SUCCESS;
}
//QUEUE FUNCTIONS
Queue* clearQueue(Queue* queue, int clear_buffer, int clear_elements_buffer){
clearList(queue->elements, clear_buffer, clear_elements_buffer);
if(clear_buffer){
free(queue);
return NULLPTR;
}
queue->last = NULLPTR;
return queue;
}
char enqueue(Queue* queue_ptr, Node* node_ptr){
if(!queue_ptr->last){
append(queue_ptr->elements, node_ptr);
//appending will not affect since there are no elements, iterating would be O(1)
queue_ptr->last = node_ptr;
return SUCCESS;
}
queue_ptr->last->next = node_ptr;
queue_ptr->last = node_ptr;
return SUCCESS;
}
Node* dequeue(Queue* queue_ptr){
if(!queue_ptr->elements->head) return NULLPTR;
Node* dequeued = queue_ptr -> elements -> head;
queue_ptr -> elements -> head = dequeued -> next;
return dequeued;
}
//STACK FUNCTIONS
char stack(Stack * stack, Node * new_node){
unshift(stack->elements, new_node);
return 0;
}
Node* unstack(Stack* stack){
Node* ptr_to_head = stack->elements->head;
stack->elements->head = stack->elements->head->next;
return ptr_to_head;
}
Node* top(Stack* stack){
if(!stack->elements->head) return NULLPTR;
return stack->elements->head;
}
Node* bottom(Stack* stack){
if(!stack->elements->head) return NULLPTR;
return last(stack->elements);
}
Stack* clearStack(Stack* stack, int clear_buffer, int clear_elements_buffer){
clearList(stack->elements, clear_buffer, clear_elements_buffer);
if(clear_buffer){
return NULLPTR;
}
return stack;
}
//BST FUNCTIONS
BST* addChild(BST* root, int child_value){
if( child_value > (root->value) ){
if(root->right_child) return addChild(root->right_child, child_value);
root->right_child = newBST(child_value);
root->right_child->father = root;
return root->right_child;
}
if( child_value < (root->value) ){
if(root->left_child) return addChild(root->left_child, child_value);
root->left_child = newBST(child_value);
root->left_child->father = root;
return root->left_child;
}
return root;
//Trying to add an equal value to a BST returns an exception.
}
BST* search(BST* root, int value){
if(!root) return ERROR;
if(root->value != value){
if(root->value > value) return search(root->left_child, value);
else return search(root->right_child, value);
}
return root;
}
BST* max(BST*root){
if(root->right_child) return max(root->right_child);
return root;
}
BST* min(BST*root){
if(root->left_child) return min(root->left_child);
return root;
}
BST* deleteChild(BST* root, BST* Child, int clear){
return deleteChildByValue(root, Child->value, clear);
}
BST* deleteChildByValue(BST* root, int value, int clear){
if(root->value == value){
BST* mark;
if(root->left_child && root->right_child){
mark = max(root->left_child);
mark->father->right_child = NULLPTR;
mark->left_child = root->left_child;
mark->right_child = root->right_child;
}
else if(root->left_child) mark = root->left_child;
else if(root->right_child) mark = root->right_child;
else mark = NULLPTR;
if(!root->father) {
root->value = mark->value;
root->left_child = mark->left_child;
root->right_child = mark->right_child;
}
else{
if(root->father->left_child == root) root->father->left_child = mark;
else root->father->right_child = mark;
root->left_child = NULLPTR;
root->right_child = NULLPTR;
root->father = NULLPTR;
if(clear){
free(root);
return NULLPTR;
}
}
return root;
}
else if(root->value > value) return deleteChildByValue(root->left_child, value, clear);
else return deleteChildByValue(root->right_child, value, clear);
}