-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
354 lines (285 loc) · 11.4 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "Arrays.h"
#include "Lists.h"
#include "Queues.h"
#include "Stacks.h"
#include "BinaryTrees.h"
#include "RedBlackTrees.h"
#include "PriorityQueues.h"
#include "DirectedGraphs.h"
#include "UndirectedGraphs.h"
#include "HashTable.h"
// THIS IS AN EXAMPLE ON HOW TO USE AND COMPILE THE LIBRARY
int main(void)
{
printf("-----------LISTS--------------------\n");
ListLink entrance = list_initialize();
list_append(entrance, 1);
list_enlist(entrance, 4);
list_append(entrance, 2);
list_enlist(entrance, 3);
list_append(entrance, 8);
list_append(entrance, 6);
list_enlist(entrance, 5);
list_append(entrance, 7);
list_print(entrance);
printf("Copying the starting list...\n");
ListLink copy = list_copy(entrance);
printf("Sorting the list:\n");
list_sort(entrance);
list_print(entrance);
printf("Reversing the list:\n");
list_reverse(entrance);
list_print(entrance);
printf("The starting was:\n");
list_print(copy);
list_destroy(copy);
list_reverse(entrance);
list_print(entrance);
list_delist(entrance);
list_print(entrance);
list_delete(entrance);
list_print(entrance);
entrance = list_destroy(entrance);
list_print(entrance);
printf("-----------STACKS--------------------\n");
StackLink pointer = stack_initialize();
stack_push(pointer, -1);
stack_push(pointer, 2);
stack_push(pointer, 8);
stack_push(pointer, 4);
stack_push(pointer, 5);
stack_push(pointer, 6);
stack_print(pointer);
stack_clear(pointer);
stack_print(pointer);
printf("-----------Queues--------------------\n");
QueueLink p = queue_initialize();
queue_enqueue(p, 13);
queue_enqueue(p, 42);
queue_enqueue(p, 24);
queue_enqueue(p, 31);
queue_enqueue(p, 18);
queue_enqueue(p, 64);
queue_enqueue(p, 75);
queue_enqueue(p, 37);
queue_print(p);
printf("Copying the starting queue...\n");
QueueLink new = queue_copy(p);
printf("Sorting the queue:\n");
queue_sort(p);
queue_print(p);
printf("Reversing the queue:\n");
queue_reverse(p);
queue_print(p);
printf("The starting was:\n");
queue_print(new);
queue_destroy(new);
queue_reverse(p);
queue_print(p);
queue_dequeue(p);
queue_print(p);
p = queue_destroy(p);
queue_print(p);
printf("-----------BINARY TREES--------------------\n");
BinaryTreeLink TreeA = binary_tree_make_tree(2);
printf("Initializing TreeA. The root item of TreeA is: %d\n", binary_tree_root(TreeA));
printf("Inserting item 1 in TreeA as left child\n");
BinaryTreeLink TreeA_One = binary_tree_insert_left(TreeA, 1);
printf("Inserting item 3 in TreeA as right child\n");
binary_tree_insert_right(TreeA, 3);
printf("The parent item of item 1 in TreeA is %d and its sibling is %d\n", binary_tree_parent(TreeA, TreeA_One), binary_tree_sibling(TreeA, TreeA_One));
printf("Height and size of TreeA are %d and %d respectively\n", binary_tree_height(TreeA), binary_tree_size(TreeA));
printf("PreOrder of TreeA is: ");
binary_tree_preorder(TreeA, item_show); printf("\n");
printf("InOrder of TreeA is: ");
binary_tree_inorder(TreeA, item_show); printf("\n");
printf("PostOrder of TreeA is: ");
binary_tree_postorder(TreeA, item_show); printf("\n");
printf("LevelOrder of TreeA is: ");
binary_tree_levelorder(TreeA, item_show); printf("\n");
printf("\n\n");
BinaryTreeLink TreeB = binary_tree_make_tree(6);
printf("Initializing TreeB. The root item of TreeB is: %d\n", binary_tree_root(TreeB));
printf("Inserting item 5 in TreeB as left child\n");
binary_tree_insert_left(TreeB, 5);
printf("Inserting item 7 in TreeB as right child\n");
BinaryTreeLink TreeB_Seven = binary_tree_insert_right(TreeB, 7);
printf("The parent of item 7 in TreeB is %d and its sibling is %d\n", binary_tree_parent(TreeB, TreeB_Seven), binary_tree_sibling(TreeB, TreeB_Seven));
printf("Height and size of TreeB are %d and %d respectively\n", binary_tree_height(TreeB), binary_tree_size(TreeB));
printf("PreOrder of TreeB is: ");
binary_tree_preorder(TreeB, item_show); printf("\n");
printf("InOrder of TreeB is: ");
binary_tree_inorder(TreeB, item_show); printf("\n");
printf("PostOrder of TreeB is: ");
binary_tree_postorder(TreeB, item_show); printf("\n");
printf("LevelOrder of TreeB is: ");
binary_tree_levelorder(TreeB, item_show); printf("\n");
printf("\n\n");
BinaryTreeLink TreeC = binary_tree_make_tree(4);
printf("Initializing TreeC. The root item of TreeC is: %d\n", binary_tree_root(TreeC));
printf("Attaching TreeA and TreeB to be left and right children of TreeC\n");
binary_tree_attach(TreeC, TreeA, TreeB);
printf("Height and size of TreeC are %d and %d respectively\n", binary_tree_height(TreeC), binary_tree_size(TreeC));
printf("PreOrder of TreeC is: ");
binary_tree_preorder(TreeC, item_show); printf("\n");
printf("InOrder of TreeC is: ");
binary_tree_inorder(TreeC, item_show); printf("\n");
printf("PostOrder of TreeC is: ");
binary_tree_postorder(TreeC, item_show); printf("\n");
printf("LevelOrder of TreeC is: ");
binary_tree_levelorder(TreeC, item_show); printf("\n\n");
printf("Searching for item 6 in TreeC... ");
if (binary_tree_search(TreeC, 6) != NULL) printf("Found!\n\n");
else printf("Not found.\n\n");
printf("Now removing 7 from TreeC\n");
binary_tree_remove(TreeC, TreeB_Seven);
printf("PreOrder of TreeC is: ");
binary_tree_preorder(TreeC, item_show); printf("\n");
printf("InOrder of TreeC is: ");
binary_tree_inorder(TreeC, item_show); printf("\n");
printf("PostOrder of TreeC is: ");
binary_tree_postorder(TreeC, item_show); printf("\n");
printf("LevelOrder of TreeC is: ");
binary_tree_levelorder(TreeC, item_show); printf("\n\n");
printf("Now removing 6 from TreeC\n");
binary_tree_remove(TreeC, TreeB);
printf("PreOrder of TreeC is: ");
binary_tree_preorder(TreeC, item_show); printf("\n");
printf("InOrder of TreeC is: ");
binary_tree_inorder(TreeC, item_show); printf("\n");
printf("PostOrder of TreeC is: ");
binary_tree_postorder(TreeC, item_show); printf("\n");
printf("LevelOrder of TreeC is: ");
binary_tree_levelorder(TreeC, item_show); printf("\n\n");
printf("Searching for item 6 in TreeC... ");
if (binary_tree_search(TreeC, 6) != NULL) printf("Found!\n");
else printf("Not found.\n");
printf("Destroying TreeC by freeing all of its nodes\n");
binary_tree_destroy(TreeC);
printf("-----------RED BLACK TREES--------------------\n");
printf("Initializing empty tree\n");
RedBlackTreeLink RB = red_black_tree_initialize();
printf("Inserting item 4\n");
red_black_tree_insert_key(&RB, 4);
printf("Inserting item 7\n");
red_black_tree_insert_key(&RB, 7);
printf("Inserting item 12\n");
red_black_tree_insert_key(&RB, 12);
printf("Inserting item 15\n");
red_black_tree_insert_key(&RB, 15);
printf("Inserting item 3\n");
red_black_tree_insert_key(&RB, 3);
printf("Inserting item 5\n");
red_black_tree_insert_key(&RB, 5);
printf("Inserting item 14\n");
red_black_tree_insert_key(&RB, 14);
printf("Inserting item 18\n");
red_black_tree_insert_key(&RB, 18);
printf("Inserting item 16\n");
red_black_tree_insert_key(&RB, 16);
printf("Inserting item 17\n");
red_black_tree_insert_key(&RB, 17);
printf("Item 12 was%sfound in tree\n", (red_black_tree_search(RB, 12) == NULLitem) ? " not " : " ");
printf("Item 33 was%sfound in tree\n", (red_black_tree_search(RB, 33) == NULLitem) ? " not " : " ");
printf("Now printing keys in ascending order using red_black_tree_print_elements: ");
red_black_tree_print_elements(RB);
printf("Removing item 3\n");
red_black_tree_remove_key(&RB, 3);
printf("Removing item 12\n");
red_black_tree_remove_key(&RB, 12);
printf("Removing item 17\n");
red_black_tree_remove_key(&RB, 17);
printf("Removing item 18\n");
red_black_tree_remove_key(&RB, 18);
printf("Removing item 15\n");
red_black_tree_remove_key(&RB, 15);
printf("Removing item 16\n");
red_black_tree_remove_key(&RB, 16);
printf("Item 12 was%sfound in tree\n", (red_black_tree_search(RB, 12) == NULLitem) ? " not " : " ");
printf("Item 33 was%sfound in tree\n", (red_black_tree_search(RB, 33) == NULLitem) ? " not " : " ");
printf("Now printing keys in ascending order using red_black_tree_print_elements: ");
red_black_tree_print_elements(RB);
printf("-----------DIRECTED GRAPHS--------------------\n");
FILE* file1;
file1 = fopen("./assets/graph_examples/DirectedGraph.txt", "r");
if (file1 == NULL) {
printf("File can't be opened. Exiting...\n");
return 1;
}
char line[100];
fgets(line,sizeof line, file1);
int size = atoi(line);
if (size > MAXVERTEX) {
printf("Sorry, max amount of vertices is %d. Exiting...\n", MAXVERTEX);
return -1;
}
DirectedGraph G = directed_graph_initialize(size);
while(fgets(line,sizeof line, file1)!= NULL) {
line[strcspn(line, "\n")] = 0;
printf("Now inserting %s\n", line);
directed_graph_insert_edge(G, line);
printf("The graph is:\n");
directed_graph_show_graph(G); printf("\n");
}
printf("\nNow reversing the graph. The reversed graph is:\n");
DirectedGraph K = directed_graph_reverse(G);
directed_graph_show_graph(K);
printf("DFS Traversal of G is: ");
directed_graph_depth_first(G, directed_graph_vertex_show);
printf("Topological sort of G is: ");
directed_graph_breadth_top_sort(G);
fclose(file1);
printf("-----------UNDIRECTED GRAPHS--------------------\n");
FILE* file2;
file2 = fopen("./assets/graph_examples/UndirectedGraph.txt", "r");
if (file2 == NULL) {
printf("File can't be opened. Exiting...\n");
return 1;
}
fgets(line,sizeof line, file2);
size = atoi(line);
if (size > MAXVERTEX) {
printf("Sorry, max amount of vertices is %d. Exiting...\n", MAXVERTEX);
return -1;
}
UndirectedGraph UG = undirected_graph_initialize(size);
while(fgets(line,sizeof line,file2)!= NULL) {
line[strcspn(line, "\n")] = 0;
printf("Now inserting %s\n", line);
undirected_graph_insert_edge(UG, line);
printf("The graph is:\n");
undirected_graph_show_graph(UG); printf("\n");
}
printf("Let's see if a simple path exists from 1 to 6\n");
undirected_graph_simple_path_check(UG, 1, 6);
printf("\nLet's see if a simple path exists from 1 to 0\n");
undirected_graph_simple_path_check(UG, 1, 0);
printf("\nLet's see if a simple path exists from 2 to 7\n");
undirected_graph_simple_path_check(UG, 2, 7);
fclose(file1);
printf("\n-----------HASH TABLE(linear probing)--------------------\n");
HashTable ht = hash_initialize();
// test insert
srand(time(NULL));
for (int i = 0; i < 1000; i++)
hash_insert(ht, i);
// test search
if (!hash_search(ht, 0))
printf("ERROR\n");
// test size
if (hash_size(ht) != 1000)
printf("ERROR\n");
// test remove
for (int i = 0; i < 1000; i++)
if (!hash_remove(ht, i))
printf("ERROR\n");
if (hash_size(ht) > 0)
printf("ERROR\n");
hash_destroy(ht);
printf("Hash Table passed all tests!\n");
return 0;
}