C Everything
This is a C repository containing a curated set of generic data structures and algorithm.
test.c
Go to the documentation of this file.
1 
6 #include "common.h"
7 #include "link_list.h"
8 #include "stack.h"
9 #include "queue.h"
10 #include "heap.h"
11 #include "tree.h"
12 #include "graph.h"
13 #include "array.h"
14 #include "disjoint_set.h"
15 
16 void test_disjoint_set();
17 void test_graph();
18 void test_array();
19 void test_tree();
20 void test_heap();
21 void test_queue();
22 void test_stack();
23 void test_linklist();
24 
31 int main(int argc, char *argv[])
32 {
33  size_t size = 0;
34  int *ptr[100];
35  int i;
36 
37  mem_init();
38  logger_init();
39  fault_manager_init(NULL);
40 
41  for (i = 0; i < 100; i ++) {
42  ptr[i] = (int *)get_mem(1, sizeof(int));
43  size += sizeof(*(ptr[i]));
44  }
45  printf("size of allocated memory = %lu\n" , size);
46  for (i = 0; i < 100; i++) {
47  free_mem(ptr[i]);
48  }
49 
50 
51  LOG_ERROR("COMMON", "Hello World\n");
52  LOG_WARN("COMMON", "Hello World\n");
53  LOG_INFO("COMMON", "Hello World\n");
54  LOG_DEBUG("COMMON", "Hello World\n");
55  LOG_TRACE_IN("COMMON", "Hello World\n");
56  LOG_TRACE_OUT("COMMON", "Hello World\n");
57 
58  test_linklist();
59  test_stack();
60  test_queue();
61  test_heap();
62  test_tree();
64  test_graph();
65  test_array();
66 
67  mem_finit();
68 
69  return 0;
70 
71 }
72 
77 void test_stack()
78 {
79  char c,*cp;
80  float f,*fp;
81  int *ip,i;
82  t_dparams dp;
83  t_stack *s1, *s2, *s3, *s4;
84 
85  // Create an array based stack to store int elements
87  s1 = create_stack("Up Stack", 10, eARRAY_STACK, &dp);
88 
89  // Create an array based stack to store char elements
90  init_data_params(&dp, eINT8);
91  s2 = create_stack("Down Stack", 10, eARRAY_STACK_DOWN, &dp);
92 
93  // Create an array based stack to store float elements
95  s3 = create_stack("Down Stack", 10, eARRAY_STACK_DOWN, &dp);
96 
97  // Create a link list based stack to store float elements
99  s4 = create_stack("Up Stack LL", 10, eLL_STACK, &dp);
100 
101  // Push elements to stack
102  for(i = 0; i < 10; i++) {
103  // Create memory for elements before adding to stack
104  s1->push(s1, assign_int(i));
105  c= 'c' + i;
106  s2->push(s2, assign_char(c));
107  f= (float)i+0.222 / 2.0f;
108  s3->push(s3, assign_float(f));
109  s4->push(s4, assign_float(f));
110  }
111  // print stack contents
112  s1->print(s1);
113  s2->print(s2);
114  s3->print(s3);
115  s4->print(s4);
116 
117  // Pop elements out of the stack
118  for(i = 0; i < 4; i++) {
119  // Free the memory created for elements before being pushed to stack
120  ip = s1->pop(s1);
121  free_mem(ip);
122  cp = s2->pop(s2);
123  free_mem(cp);
124  fp = s3->pop(s3);
125  free_mem(fp);
126  fp = s4->pop(s4);
127  free_mem(fp);
128  }
129  // print stack contents
130  s1->print(s1);
131  s2->print(s2);
132  s3->print(s3);
133  s4->print(s4);
134 
135  // Push elements to stack
136  for(i = 4; i > 0; i--) {
137  s1->push(s1, assign_int(i));
138  c= 'c' + i;
139  s2->push(s2, assign_int(c));
140  f= (float)i+0.222 / 2.0f;
141  s3->push(s3, assign_float(f));
142  s4->push(s4, assign_float(f));
143  }
144  // print stack contents
145  s1->print(s1);
146  s2->print(s2);
147  s3->print(s3);
148  s4->print(s4);
149 
150  // Destroy stack
151  s1->destroy(s1);
152  s2->destroy(s2);
153  s3->destroy(s3);
154  s4->destroy(s4);
155 }
156 
162 {
163  char c,*cp,*sp,str[][64] = {"I", "See", "Everyting"};
164  float f,*fp;
165  int *ip, i;
166  t_dparams dp;
167  t_linklist *l1, *l2, *l3, *l4, *l5;
168 
169  // Create a double link list to store int elements
170  init_data_params(&dp, eINT32);
171  l1 = create_link_list("INT DLL",eDOUBLE_LINKLIST, &dp);
172 
173  // Create a single link list to store char elements
174  init_data_params(&dp, eINT8);
175  l2 = create_link_list("CHAR SLL",eSINGLE_LINKLIST, &dp);
176 
177  // Create a single circular link list to store float elements
178  init_data_params(&dp, eFLOAT);
179  l3 = create_link_list("FLOAT SCLL",eSINGLE_CIRCULAR_LINKLIST, &dp);
180 
181  // Create a doubly circular link list to store string elements
183  l4 = create_link_list("STRING DCLL",eDOUBLE_CIRCULAR_LINKLIST, &dp);
184 
185  // Create a xor link list to store float elements
186  init_data_params(&dp, eFLOAT);
187  l5 = create_link_list("FLOAT XORLL",eXOR_LINKLIST, &dp);
188 
189  // Add elements to link list (add elemnts to begining)
190  for (i = 0; i < 3; i++) {
191  c= 'c' + i;
192  l2->add(l2, assign_char(c));
193 
194  l1->add(l1, assign_int(i));
195 
196  f= (float)i+0.222 / 2.0f;
197  l3->add(l3, assign_float(f));
198  l5->add(l5, assign_float(f));
199 
200  l4->add(l4, assign_string(str[i]));
201  }
202  // print elements of link list
203  l1->print(l1);
204  l2->print(l2);
205  l3->print(l3);
206  l4->print(l4);
207  l5->print(l5);
208 
209  // find an element in the link list
210  t_gen tmp;
211  tmp = l4->find(l4, "SEe");
212  if (tmp != NULL) {
213  printf("Data present \n");
214  }
215  else {
216  printf("Data not present \n");
217  }
218 
219  // Search and delete a given element in link list
220  LOG_INFO("TEST", "deleting nodes in link list\n");
221  for (i = 0; i < 3; i++) {
222  c= 'c' + i;
223  cp = l2->del(l2, &c);
224  free_mem(cp);
225 
226  ip = l1->del(l1, &i);
227  free_mem(ip);
228 
229  f= (float)i+0.222 / 2.0f;
230  fp = l3->del(l3, &f);
231  free_mem(fp);
232 
233  sp = l4->del(l4, &str[i]);
234  free_mem(sp);
235 
236  fp = l5->del(l5, &f);
237  free_mem(fp);
238  }
239  // print elements of link list
240  l1->print(l1);
241  l2->print(l2);
242  l3->print(l3);
243  l4->print(l4);
244  l5->print(l5);
245 
246  // Add elements to link list (add elemnts to tail of link list)
247  for (i = 0; i < 3; i++) {
248  c= 'c' + i;
249  l2->append(l2, assign_char(c));
250 
251  l1->append(l1, assign_int(i));
252 
253  f= (float)i+0.222 / 2.0f;
254  l3->append(l3, assign_float(f));
255 
256  l4->append(l4, assign_string(str[i]));
257  l5->append(l5, assign_float(f));
258  }
259  // print elements of link list
260  l1->print(l1);
261  l2->print(l2);
262  l3->print(l3);
263  l4->print(l4);
264  l5->print(l5);
265 
266  // Add elements to link list (add elemnts to head of link list)
267  for (i = 3; i < 13; i++) {
268  c= 'c' + i;
269  l2->add(l2, assign_char(c));
270 
271  l1->add(l1, assign_int(i));
272 
273  f= (float)i+0.222 / 2.0f;
274  l3->add(l3, assign_float(f));
275  l5->add(l5, assign_float(f));
276  }
277  // print elements of link list
278  l1->print(l1);
279  l2->print(l2);
280  l3->print(l3);
281  l4->print(l4);
282  l5->print(l5);
283 
284  // Delete the node at idx 'i'th index
285  for (i = 2; i >= 0; i--) {
286  cp = l2->del_idx(l2, i);
287  free_mem(cp);
288 
289  ip = l1->del_idx(l1, i);
290  free_mem(ip);
291 
292  fp = l3->del_idx(l3, i);
293  free_mem(fp);
294 
295  sp = l4->del_idx(l4, i);
296  free_mem(sp);
297 
298  fp = l5->del_idx(l5, i);
299  free_mem(fp);
300  }
301  // print elements of link list
302  l1->print(l1);
303  l2->print(l2);
304  l3->print(l3);
305  l4->print(l4);
306  l5->print(l5);
307 
308  // Destroy link list
309  l1->destroy(l1);
310  l2->destroy(l2);
311  l3->destroy(l3);
312  l4->destroy(l4);
313  l5->destroy(l5);
314 }
315 
321 {
322  char c,*cp;
323  float f,*fp;
324  int i;
325  t_dparams dp;
326  t_queue *q1, *q2;
327 
328  // Create a queue to store char elements
329  init_data_params(&dp, eINT8);
330  q1 = create_queue("Queue1", 10, eARRAY_QUEUE_CIRC, &dp);
331 
332  // Create a queue to store float elements
333  init_data_params(&dp, eFLOAT);
334  q2 = create_queue("Queue2", 10, eLL_QUEUE_CIRC, &dp);
335 
336  // add elements to queue
337  for(i = 0; i < 10; i++) {
338  c= 'c' + i;
339  q1->enq(q1, assign_char(c));
340 
341  f= (float)i+0.222 / 2.0f;
342  q2->enq(q2, assign_float(f));
343  }
344  // Print elements in queue
345  q1->print(q1);
346  q2->print(q2);
347 
348  // Dequeue or delete queue elements
349  for(i = 0; i < 10; i++) {
350  cp = q1->deq(q1);
351  free_mem(cp);
352  fp = q2->deq(q2);
353  free_mem(fp);
354  }
355  // Print elements in queue
356  q1->print(q1);
357  q2->print(q2);
358 
359  // add elements to queue
360  for(i = 0; i < 4; i++) {
361  c= 'c' + i;
362  q1->enq(q1, assign_char(c));
363  f= (float)i+0.222 / 2.0f;
364  q2->enq(q2, assign_float(f));
365  }
366  printf("\n");
367 
368  // Print elements in queue
369  q1->print(q1);
370  q2->print(q2);
371 
372  // Peek element of the queue
373  fp = q2->peek(q2, 2);
374  printf("peeking idx <2: %f>\n", *fp);
375 
376  // Destroy queue
377  q1->destroy(q1);
378  q2->destroy(q2);
379 }
380 
385 void test_heap()
386 {
387  float f, arr1[10]={0};
388  int arr[10] = {1,53,32,43,3,23,11,209, -2, 25};
389  char carr[10] = {'&', '^', 'j', 'a', 'r', 'e', 'm', '*', '%', '!'};
390  t_dparams dp;
391  t_heap *h1, *h2, *h3;
392 
393  // Create a int heap
394  init_data_params(&dp, eINT32);
395  h1 = create_heap("INT HEAP", arr,10, eMIN_HEAP, &dp);
396 
397  // Create a char heap
398  init_data_params(&dp, eINT8);
399  h2 = create_heap("CHAR HEAP", carr,10, eMAX_HEAP, &dp);
400 
401  // Create a float heap
402  init_data_params(&dp, eFLOAT);
403  h3 = create_heap("float HEAP", arr1,10, eMIN_HEAP, &dp);
404 
405  // Insert element
406  f = -1.2234f;
407  h3->insert(h3,&f);
408  f = -10.3242f;
409  h3->insert(h3,&f);
410  f = 1000.2f;
411  h3->insert(h3,&f);
412  f = 1.2f;
413  h3->insert(h3,&f);
414  f = -9.2f;
415  h3->insert(h3,&f);
416  f = 9.2f;
417  h3->insert(h3,&f);
418 
419  // heap sort data;
420  printf("Sorting\n");
421  h1->sort(h1);
422  h2->sort(h2);
423 
424 
425  // Print heap contents
426  h1->print(h1);
427  h2->print(h2);
428  h3->print(h3);
429 
430  // heapify array
431  h1->build(h1);
432  h2->build(h2);
433 
434  // Update key
435  f = 2000.01f;
436  h3->update(h3, &f, 2);
437  printf("Heapify'd array\n");
438  f = -2000.01f;
439  h3->update(h3, &f, 2);
440 
441  // Extract Min from heap
442  printf("%d\n", *(int*)(h1->extract(h1)));
443  printf("%d\n", *(int*)(h1->extract(h1)));
444 
445  // Print heap contents
446  h1->print(h1);
447  h2->print(h2);
448  h3->print(h3);
449 
450  // Destroy heap
451  h1->destroy(h1);
452  h2->destroy(h2);
453  h3->destroy(h3);
454 
455 }
456 
461 void test_tree()
462 {
463  char c,*cp,str[][64] = {"I", "See", "Everyting"};
464  char carr[10] = {'&', '^', 'j', 'a', 'r', 'e', 'm', '*', '%', '!'};
465  float f,*fp;
466  int i;
467  t_dparams dp;
468  t_tree_node *max, *min, *pred, *succ;
469  t_tree *t1, *t2, *t3, *t4;
470 
471  // Create a BST tree to store character values
472  init_data_params(&dp, eINT8);
473  t1 = create_tree("tree1", eBST, &dp);
474 
475  // Create a AVL tree to store float values
476  init_data_params(&dp, eFLOAT);
477  t2 = create_tree("tree2", eAVL, &dp);
478 
479  // Create a AVL tree to store STRING values
481  t3 = create_tree("tree3", eAVL, &dp);
482 
483  // Create a BST tree to store float values
484  init_data_params(&dp, eFLOAT);
485  t4 = create_tree("tree4", eBST, &dp);
486 
487  // Insert elements to trees
488  for(i = 0; i < 3; i++) {
489  // Dynammically alocate memory and store the variable to tree
490  t1->insert(t1, assign_char(carr[i]));
491  f= (float)i+0.222 / 2.0f;
492  t2->insert(t2, assign_float(f));
493  t3->insert(t3, assign_string(str[i]));
494  t4->insert(t4, assign_float(f));
495  }
496 
497  // Insert elements to trees
498  for(i = 9; i >= 3; i--) {
499  t1->insert(t1, assign_char(carr[i]));
500  f= (float)i+0.222 / 2.0f;
501  t2->insert(t2, assign_float(f));
502  t4->insert(t4, assign_float(f));
503  }
504 
505  // Find an element in tree
506  char str1[10] = "some";
507  if (t3->find(t3, str1) == NULL) {
508  printf("%s not present in tree\n", str1);
509  } else {
510  printf("%s present in tree\n", str1);
511  }
512 
513  // Find an element in tree
514  char str2[10] = "I";
515  if (t3->find(t3, str2) == NULL) {
516  printf("%s not present in tree\n", str2);
517  } else {
518  printf("%s present in tree\n", str2);
519  }
520 
521  // Min Max node in tree
522  printf("\nmin & max\n");
523  max = t1->max(t1->root);
524  min = t1->min(t1->root);
525  printf("%c %c\n", *(char*) max->key, *(char*)min->key);
526 
527  max = t2->max(t2->root);
528  min = t2->min(t2->root);
529  printf("%f %f\n", *(float*)max->key,*(float*)min->key);
530 
531  max = t3->max(t3->root);
532  min = t3->min(t3->root);
533  printf("%s %s\n", (char*)max->key, (char*) min->key);
534 
535  // Succ & pred in tree
536  printf("\nPred & Succ\n");
537  c = 'm';
538  pred = t1->pred(t1,&c);
539  succ = t1->succ(t1,&c);
540  printf("%c %c\n", *(char*) pred->key, *(char*)succ->key);
541 
542  f = 2.111000f;
543  pred = t2->pred(t2,&f);
544  succ = t2->succ(t2,&f);
545  printf("%f %f\n", *(float*) pred->key, *(float*)succ->key);
546 
547  pred = t3->pred(t3,str[0]);
548  succ = t3->succ(t3,str[0]);
549  printf("%s %s\n", (char*) pred->key, (char*)succ->key);
550 
551  // Get height of the tree
552  pred = t2->root;
553  succ = t4->root;
554  printf("Tree height avl/bst %d %d\n",
555  t2->height(pred), t4->height(succ));
556  // Inorder traverse the tree nodes
557  t1->inorder(t1);
558  t2->inorder(t2);
559  t3->inorder(t3);
560  t4->inorder(t4);
561 
562  // preorder traverse the tree nodes
563  t1->preorder(t1);
564  t2->preorder(t2);
565  t3->preorder(t3);
566  t4->preorder(t4);
567 
568  // postorder traverse the tree nodes
569  t1->postorder(t1);
570  t2->postorder(t2);
571  t3->postorder(t3);
572  t4->postorder(t4);
573 
574  // Print the tree nodes
575  t1->print(t1);
576  t2->print(t2);
577  t3->print(t3);
578  t4->print(t4);
579 
580  // deleting nodes
581  for(i = 9; i >= 0; i--) {
582  cp = t1->del(t1, &carr[i]);
583  free_mem(cp);
584  f= (float)i+0.222 / 2.0f;
585  fp = t2->del(t2, &f);
586  free_mem(fp);
587  }
588 
589  t1->destroy(t1);
590  t2->destroy(t2);
591  t3->destroy(t3);
592  t4->destroy(t4);
593 }
594 
600 {
601  t_graph *g1, *g2, *g3, *g4, *g5;
602  char city[][64] ={"Delhi", "Bangalore","Chennai"};
603  int i, *ip, num[] = {1,2,3,4,5,6,7,8,9,10};
604  int a1[] = {1,2,3,4,5,6,7,8,9,10,11,12};
605  int a2[] = {1,2,3,4,5,6,7,8,9,10};
606  int a3[] = {1,2,3,4,5,6,7,8,9,10};
607  t_dparams dp;
608  t_bfsinfo *bfs;
609  t_dfsinfo *dfs;
610  t_daginfo *dag;
611 
612  // create data opertation set of type 'String'
614  // Dummy free since the elements are part of an array and not dynamically allocated
615  dp.free = dummy_free;
616  // Create a graph to store strings
617  g1 = create_graph("Graph 1", 10, &dp);
618 
619  init_data_params(&dp, eINT32);
620  dp.free = dummy_free;
621  g2 = create_graph("Graph 2", 10, &dp);
622 
623  init_data_params(&dp, eINT32);
624  dp.free = dummy_free;
625  g3 = create_graph("Graph 3", 12, &dp);
626 
627  // create data opertation set of type 'int'
628  init_data_params(&dp, eINT32);
629  dp.free = dummy_free;
630  g4 = create_graph("Graph 4", 10, &dp);
631 
632  // create data opertation set of type 'int'
633  init_data_params(&dp, eINT32);
634  // Dummy free since the elements are part of an array
635  dp.free = dummy_free;
636  // Create a graph to store int values
637  g5 = create_graph("Graph 5", 10, &dp);
638 
639  // Create symmetric edges between vertices
640  g1->add_wedge_sym(g1, city[0], city[1], 420);
641  g1->add_wedge_sym(g1, city[1], city[2], 1234);
642  g1->add_wedge(g1, city[2], city[0], 1000);
643  g1->wprint(g1);
644 
645  // Check whether the edge between two vertices exist?
646  if (g1->has_edge(g1, city[0], city[2]) == NULL) {
647  printf("%s not linked to %s\n",city[0],city[1]);
648  } else {
649  printf("%s linked to %s\n",city[0],city[1]);
650  }
651 
652  // Delete the directed edge between two nodes
653  g1->del_edge(g1, city[0], city[1]);
654 
655  // Delete an undirected edge between two nodes
656  g1->del_edge_sym(g1, city[2], city[1]);
657  g1->wprint(g1);
658 
659  // Delete a Vertex from graph
660  g1->del_vertex(g1, city[2]);
661  g1->wprint(g1);
662 
663  if (g1->find(g1, city[2]) == NULL) {
664  printf("%s not present in graph\n",city[2]);
665  } else {
666  printf("%s present in graph\n",city[2]);
667  }
668  // weighted print graph
669  g1->wprint(g1);
670 
671  printf("**************\n");
672 
673  printf("- Connected Components -\n");
674 
675  // Add nodes to graph
676  for (i = 0; i < 12; i++) {
677  g3->add_vertex(g3, &a1[i]);
678  }
679 
680  // Create symmetric edges between vertices
681  g3->add_edge_sym(g3, &a1[0], &a1[1]);
682  g3->add_edge_sym(g3, &a1[0], &a1[4]);
683  g3->add_edge_sym(g3, &a1[4], &a1[8]);
684  g3->add_edge_sym(g3, &a1[4], &a1[9]);
685  g3->add_edge_sym(g3, &a1[2], &a1[3]);
686  g3->add_edge_sym(g3, &a1[2], &a1[6]);
687  g3->add_edge_sym(g3, &a1[2], &a1[7]);
688  g3->add_edge_sym(g3, &a1[3], &a1[7]);
689  g3->add_edge_sym(g3, &a1[6], &a1[7]);
690  g3->add_edge_sym(g3, &a1[6], &a1[10]);
691  g3->add_edge_sym(g3, &a1[7], &a1[10]);
692  g3->add_edge_sym(g3, &a1[7], &a1[11]);
693  g3->add_edge_sym(g3, &a1[8], &a1[9]);
694  g3->print(g3);
695 
696  // Run BFS on GRAPH
697  printf("- BFS -\n");
698  bfs = g3->bfs(g3, &a1[0]);
699 
700  // Display the BFS Tree
701  for(i = 0; i < 12; i++) {
702  ip = (int*)bfs[i].parent;
703  printf("%d: %d %d %d\n",
704  bfs[i].comp,i+1, bfs[i].level, ip != NULL? *ip: -1);
705  }
706  free_mem(bfs);
707 
708  // Run DFS on GRAPH
709  printf("- DFS -\n");
710  dfs = g3->dfs(g3, &a1[0]);
711 
712  // Display the DFS Tree
713  for(i = 0; i < 12; i++) {
714  ip = (int*)dfs[i].parent;
715  printf("%d: %d %d {%d %d}\n",
716  dfs[i].comp, i+1, ip != NULL? *ip: -1, dfs[i].pre, dfs[i].post);
717  }
718  free_mem(dfs);
719 
720  // Add nodes to graph
721  for (i = 0; i < 8; i++) {
722  g4->add_vertex(g4, &a2[i]);
723  }
724 
725  // Create asymmetric edges
726  g4->add_edge(g4, &a2[0], &a2[2]);
727  g4->add_edge(g4, &a2[0], &a2[3]);
728  g4->add_edge(g4, &a2[0], &a2[4]);
729  g4->add_edge(g4, &a2[1], &a2[2]);
730  g4->add_edge(g4, &a2[1], &a2[7]);
731  g4->add_edge(g4, &a2[2], &a2[5]);
732  g4->add_edge(g4, &a2[3], &a2[7]);
733  g4->add_edge(g4, &a2[3], &a2[5]);
734  g4->add_edge(g4, &a2[4], &a2[7]);
735  g4->add_edge(g4, &a2[5], &a2[6]);
736  g4->add_edge(g4, &a2[6], &a2[7]);
737  g4->print(g4);
738 
739  printf("- DAGS Sorting and Longest Path -\n");
740  dag = g4->topo_order_dag(g4);
741 
742  // Display the longest path and ordered/sorted edges of the DAG
743  for(i = 0; i < 8; i++) {
744  ip = (int*)dag[i].node;
745  printf("{%d %d}\n",
746  ip != NULL? *ip: -1, dag[i].longest_path);
747  }
748  free_mem(dag);
749 
750  // Add nodes to graph
751  for (i = 0; i < 10; i++) {
752  g2->add_vertex(g2, &num[i]);
753  }
754 
755  // Create symmetric weighted edges
756  g2->add_wedge_sym(g2, &num[0], &num[1], 10);
757  g2->add_wedge_sym(g2, &num[0], &num[2], 80);
758  g2->add_wedge_sym(g2, &num[1], &num[2], 6);
759  g2->add_wedge_sym(g2, &num[2], &num[3], 70);
760  g2->add_wedge_sym(g2, &num[1], &num[4], 20);
761  g2->add_wedge_sym(g2, &num[4], &num[5], 50);
762  g2->add_wedge_sym(g2, &num[4], &num[6], 10);
763  g2->add_wedge_sym(g2, &num[5], &num[6], 5);
764  g2->wprint(g2);
765 
766  // Run Dijkstra's algo for src node '1'
767  printf("* Dijkstra's Algo *\n");
768  t_distinfo *dist = dijkstra(g2, &num[0]);
769 
770  // Display the Distance table with parent node and cost from src
771  for (i = 0; i < 10; i++) {
772  printf("{%d : %d %d}\n",
773  dist[i].edge.node?((t_gnode*)dist[i].edge.node)->idx+1: -1,
774  dist[i].edge.weight,
775  dist[i].parent? ((t_gnode*)dist[i].parent)->idx+1: -1);
776  }
777  free_mem(dist);
778 
779  // Run Prim's MST algo
780  printf("* Prim's Minimum Spanning Tree *\n");
781  dist = prims_mst(g2);
782 
783  // Display the Prim's MST
784  for (i = 0; i < 10; i++) {
785  printf("{%d - %d: %d}\n",
786  dist[i].edge.node?((t_gnode*)dist[i].edge.node)->idx+1: -1,
787  dist[i].parent? ((t_gnode*)dist[i].parent)->idx+1:
788  -1, dist[i].edge.weight);
789  }
790  free_mem(dist);
791 
792  // Run Kruskal's MST algo
793  printf("* Kruskal's Minimum Spanning Tree *\n");
794  dist = kruskals_mst(g2);
795 
796  // Display the Kruskal MST
797  for (i = 0; i < 10; i++) {
798  printf("{%d - %d: %d}\n",
799  dist[i].edge.node?((t_gnode*)dist[i].edge.node)->idx+1: -1,
800  dist[i].parent? ((t_gnode*)dist[i].parent)->idx+1:
801  -1, dist[i].edge.weight);
802  }
803  free_mem(dist);
804 
805 
806  // Add nodes to graph
807  for (i = 0; i < 10; i++) {
808  g5->add_vertex(g5, &a3[i]);
809  }
810 
811  // Add nodes to graph
812  g5->add_wedge(g5, &a3[0], &a3[1], 10);
813  g5->add_wedge(g5, &a3[0], &a3[7], 8);
814  g5->add_wedge(g5, &a3[1], &a3[5], 2);
815  g5->add_wedge(g5, &a3[2], &a3[1], 1);
816  g5->add_wedge(g5, &a3[2], &a3[3], 1);
817  g5->add_wedge(g5, &a3[3], &a3[4], 3);
818  g5->add_wedge(g5, &a3[4], &a3[5], -1);
819  g5->add_wedge(g5, &a3[5], &a3[2], -2);
820  g5->add_wedge(g5, &a3[6], &a3[5], -1);
821  g5->add_wedge(g5, &a3[6], &a3[1], -4);
822  g5->add_wedge(g5, &a3[7], &a3[6], 1);
823  g5->wprint(g5);
824 
825  // Bellman Ford shortest path src node as 0
826  dist = bellman_ford(g5, &a3[0]);
827 
828  printf("* Bellman Ford *\n");
829  // print the distance
830  for (i = 0; i < 10; i++) {
831  printf("{%d : %d %d}\n",
832  dist[i].edge.node?((t_gnode*)dist[i].edge.node)->idx+1: -1,
833  dist[i].edge.weight,
834  dist[i].parent? ((t_gnode*)dist[i].parent)->idx+1: -1);
835  }
836  free_mem(dist);
837 
838  // destroy graph
839  g1->destroy(g1);
840  g2->destroy(g2);
841  g3->destroy(g3);
842  g4->destroy(g4);
843  g5->destroy(g5);
844 }
845 
851 {
852  int a1[] = {310,-22,35,534,55,76,907,8,239,1210,151,-12};
853  int a2[] = {310,-22,35,534,55,76,907,8,239,1210,151,-12};
854  int a3[] = {310,-22,35,534,55,76,907,8,239,1210,151,-12};
855  t_dparams dp;
856 
857  // Create operating fn depending on type of data
858  init_data_params(&dp, eINT32);
859 
860  // select sort array
861  selection_sort(a1, 12, &dp);
862 
863  printf("Select sort: \n");
864  for (int i = 0; i < 12; i++) {
865  printf("%d ", a1[i]);
866  }
867  printf("\n");
868 
869  // insertion sort array
870  insertion_sort(a2, 12, &dp);
871 
872  printf("Insertion sort: \n");
873  for (int i = 0; i < 12; i++) {
874  printf("%d ", a2[i]);
875  }
876  printf("\n");
877 
878  // quick sort array
879  quick_sort(a3, 12, &dp);
880 
881  printf("quick sort: \n");
882  for (int i = 0; i < 12; i++) {
883  printf("%d ", a3[i]);
884  }
885  printf("\n");
886 
887 
888 }
889 
895 {
896  // Create a disjoint set
897  t_disjset *d = create_disjoint_set("disjoint set", 10);
898 
899  printf("* Disjoint set operation test *\n");
900 
901  // Make set i.e, every elem point to self as it component
902  d->make(d);
903 
904  // merge two components
905  d->merge(d, 1, 2);
906  d->merge(d, 4, 3);
907  d->merge(d, 3, 2);
908  d->merge(d, 5, 7);
909 
910  // print set
911  d->print(d);
912 
913  // find the component the element belongs to
914  printf("find 2: %d\n",d->find(d, 2));
915  printf("find 1: %d\n",d->find(d, 4));
916  printf("find 8: %d\n",d->find(d, 8));
917  // print set
918  d->print(d);
919 
920  // Destroy disjoint set
921  d->destroy(d);
922 }
void insertion_sort(t_gen a, int n, t_dparams *op)
Insertion sort builds the final sorted array one item at a time. It has an O(n2) time complexity
Definition: array.c:67
void quick_sort(t_gen a, int n, t_dparams *op)
Quicksort is an in-place sorting algorithm is a divide and conquer algorithm which relies on a partit...
Definition: array.c:122
void selection_sort(t_gen a, int n, t_dparams *op)
Selection sort is an in-place comparison sorting algorithm. It has an O(n2) time complexity
Definition: array.c:40
Contains declations of array operations and structure.
Top level include containg common headers.
void init_data_params(t_dparams *, e_data_types)
Called to initalize data params for default data types Else for custom data types the data params str...
Definition: common.c:15
void dummy_free(void *mem_addr, char *file, int line)
Dummy free function Else for custom data types the data params structure is to be defined by user
Definition: common.c:75
t_gen create_disjoint_set(char *name, int size)
Create an instance of disjoint set data struct
Definition: disjoint_set.c:20
Contains declations of disjoint_set types, operations and structure.
void fault_manager_init(f_fault_handle h)
Handles signals and faults depending on type of fault/signal occured
Definition: fault_manager.c:52
t_gen assign_float(float)
t_gen assign_char(char)
t_gen assign_string(char *)
Definition: generic_def.c:43
t_gen assign_int(int)
t_gen dijkstra(t_gen d, t_gen data)
Utils function used by quick sort for swapping graph edges
Definition: graph.c:1043
t_gen kruskals_mst(t_gen d)
Find the Minimum Spanning for weighted undirected graph Using Kruskal's Algorithm
Definition: graph.c:1295
t_gen create_graph(char *name, int size, t_dparams *prm)
Create an instance of graph
Definition: graph.c:42
t_gen prims_mst(t_gen d)
Find the Minimum Spanning for weighted undirected graph Using Prim's Algorithm
Definition: graph.c:1217
t_gen bellman_ford(t_gen d, t_gen data)
Find the shortest path from a given source vertex to all source nodes in a graph with negative edges ...
Definition: graph.c:1135
Contains declations of graph types, operations and structure.
t_gen create_heap(char *name, t_gen data, int size, e_heaptype htype, t_dparams *prm)
Create an instance of heap
Definition: heap.c:29
Contains declations of heap types, operations and structure.
@ eMIN_HEAP
Minimum Heap
Definition: heap.h:10
@ eMAX_HEAP
Maximum Heap.
Definition: heap.h:11
#define LOG_INFO(mod, fmt, args...)
Definition: logger.h:19
#define LOG_TRACE_IN(mod, fmt, args...)
Definition: logger.h:21
#define LOG_DEBUG(mod, fmt, args...)
Definition: logger.h:20
#define LOG_TRACE_OUT(mod, fmt, args...)
Definition: logger.h:22
#define LOG_ERROR(mod, fmt, args...)
Definition: logger.h:17
void logger_init()
Initailize logger module
Definition: logger.c:17
#define LOG_WARN(mod, fmt, args...)
Definition: logger.h:18
void mem_init(void)
Initailize memory module
void mem_finit(void)
Close memory module by checking and destroying if any tagged memory
#define free_mem(mem_addr)
Definition: memory_manager.h:9
#define get_mem(nmemb, size)
Definition: memory_manager.h:8
t_gen create_queue(char *name, int max_size, e_queuetype qtype, t_dparams *prm)
Destroy queue instance
Definition: queue.c:37
Contains declations of queue types, operations and structure.
@ eLL_QUEUE_CIRC
Link List Based Queue.
Definition: queue.h:12
@ eARRAY_QUEUE_CIRC
Array Based Queue.
Definition: queue.h:13
t_gen create_stack(char *name, int max_size, e_stacktype stype, t_dparams *prm)
Create an instance of stack
Definition: stack.c:37
Contains declations of stack types, operations and structure.
@ eARRAY_STACK
Top Growing Stack.
Definition: stack.h:14
@ eLL_STACK
LinkList based Stack.
Definition: stack.h:13
@ eARRAY_STACK_DOWN
Down Growing Stack.
Definition: stack.h:15
Level and Parent info after BFS walk.
Definition: graph.h:64
Longest Path and Node order after DAG ordering.
Definition: graph.h:80
data params struct defn
Definition: common.h:18
f_free free
Routine used for freeing elements of said data.
Definition: common.h:26
Level and Parent info after DFS walk.
Definition: graph.h:71
Disjoint set main struct definition.
Definition: disjoint_set.h:19
f_print print
routine to print elements in the disjoint set
Definition: disjoint_set.h:28
f_set2 merge
routine to merge to set
Definition: disjoint_set.h:26
f_destroy destroy
routine to destroy the instace of disjoint set
Definition: disjoint_set.h:29
f_set1 find
routine to find an elem in the set
Definition: disjoint_set.h:25
f_vgen make
routine to add an new elem to the set
Definition: disjoint_set.h:24
Dist info.
Definition: graph.h:87
t_gnode * parent
Pointer to Vertex vertex.
Definition: graph.h:89
t_gedge edge
Pointer to edge.
Definition: graph.h:88
int weight
Cost of the edge.
Definition: graph.h:19
t_gnode * node
Pointer to neighbor vertex.
Definition: graph.h:18
graph Vertex
Definition: graph.h:10
int idx
Index of vertex in adaceny list.
Definition: graph.h:12
graph struct defn
Definition: graph.h:26
f_find find
routine to find a vertex in graph
Definition: graph.h:50
f_print wprint
routine to print graph info with edge weights
Definition: graph.h:53
f_gen3 del_edge_sym
routine to del a symmetric edge in graph
Definition: graph.h:42
f_print print
routine to print graph info
Definition: graph.h:52
f_wedge add_wedge_sym
routine to add a weighted symmetric edge in graph
Definition: graph.h:44
f_destroy destroy
routine to destroy the graph instance
Definition: graph.h:54
f_gen3 add_edge_sym
routine to add a symmetric edge in graph
Definition: graph.h:41
f_gen3 del_edge
routine to del an edge in graph
Definition: graph.h:40
f_gen3 has_edge
routine to check an edge between two vertices graph
Definition: graph.h:45
f_gen3 add_edge
routine to add an edge in graph
Definition: graph.h:39
f_wedge add_wedge
routine to add a weighted edge in graph
Definition: graph.h:43
f_gen2 bfs
routine to Breadth First Search in graph
Definition: graph.h:46
f_gen2 del_vertex
routine to del a vertex in graph
Definition: graph.h:38
f_gen2 dfs
routine to Depth First Search in graph
Definition: graph.h:47
f_gen topo_order_dag
routine to topologica order a DAG
Definition: graph.h:49
f_gen2 add_vertex
routine to add a vertex in graph
Definition: graph.h:37
Heap struct defn.
Definition: heap.h:18
f_update update
routine to update a key of given heap node
Definition: heap.h:32
f_ins insert
routine to insert elements in heap
Definition: heap.h:28
f_vgen sort
routine to heap sort
Definition: heap.h:31
f_print print
routine to print heap info
Definition: heap.h:36
f_vgen build
routine to heapify
Definition: heap.h:30
f_destroy destroy
routine to destroy
Definition: heap.h:37
f_gen extract
routine to extract min/max root element in heap
Definition: heap.h:29
stack struct defn
Definition: stack.h:19
f_vgen destroy
routine to destroy stack contents
Definition: stack.h:36
f_print print
routine to print stack contents
Definition: stack.h:35
f_gen2 push
stack operations
Definition: stack.h:29
f_gen pop
routine to pop element into stack
Definition: stack.h:30
queue struct defn
Definition: queue.h:17
f_print print
routine to print queue elements
Definition: queue.h:35
f_gen deq
routine to pop elements out of queue
Definition: queue.h:30
f_destroy destroy
routine to detroy queue instance
Definition: queue.h:36
f_genidx peek
routine to peek node in queue
Definition: queue.h:32
f_ins enq
routine to push elements to queue
Definition: queue.h:29
tree node
Definition: tree.h:9
t_gen key
Pointer to node key.
Definition: tree.h:10
tree struct defn
Definition: tree.h:24
f_gen min
routine to get minm element in tree
Definition: tree.h:39
f_find find
routine to find element in tree
Definition: tree.h:36
f_len height
routine to get height of tree
Definition: tree.h:41
f_gen2 pred
routine to get predecessor to given node
Definition: tree.h:37
f_ins insert
routine to insert element in tree
Definition: tree.h:34
f_print print
routine to print tree level by level
Definition: tree.h:46
f_print inorder
routine to print inorder traversal of tree
Definition: tree.h:43
f_gen max
routine to get maxm element in tree
Definition: tree.h:40
f_destroy destroy
routine to destroy the tree instance
Definition: tree.h:47
f_print preorder
routine to print preorder traversal of tree
Definition: tree.h:44
t_gen root
Root node of the tree.
Definition: tree.h:31
f_gen2 succ
routine to get successor to given node
Definition: tree.h:38
f_del del
routine to delete element in tree
Definition: tree.h:35
f_print postorder
routine to print postorder traversal of tree
Definition: tree.h:45
void test_array()
Test Array search and sort routines
Definition: test.c:850
void test_tree()
Test Tree routines
Definition: test.c:461
int main(int argc, char *argv[])
Main Driver test
Definition: test.c:31
void test_heap()
Test Heap routines
Definition: test.c:385
void test_graph()
Test Graph routines
Definition: test.c:599
void test_linklist()
Test link list routines
Definition: test.c:161
void test_queue()
Test Queue routines
Definition: test.c:320
void test_stack()
Test stack routines
Definition: test.c:77
void test_disjoint_set()
Test Disjoint set routines (Merge-Find)
Definition: test.c:894
t_gen create_tree(char *name, e_treetype ttype, t_dparams *prm)
Create an instance of tree
Definition: tree.c:45
Contains declations of tree types, operations and structure.
@ eBST
Binary Search Tree.
Definition: tree.h:18
@ eAVL
AVL Tree.
Definition: tree.h:19
void * t_gen
Base Data type used for all data structure and data elements.
Definition: typedefs.h:41
@ eFLOAT
Definition: typedefs.h:26
@ eSTRING
Definition: typedefs.h:28
@ eINT32
Definition: typedefs.h:24
@ eINT8
Definition: typedefs.h:20