Advertisement
kirya_shkolnik

WTF?

Feb 10th, 2023
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.89 KB | None | 0 0
  1. // documentation_module.c
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "documentation_module.h"
  6.  
  7. int *check_available_documentation_module(int (*validate)(char*), int count, char **documents)
  8. {
  9.     int *availability_mask = malloc(count * sizeof(int));
  10.     for (int i = 0; i < count; i++) {
  11.         char *document = documents[i];
  12.         availability_mask[i] = validate(document);
  13.     }
  14.  
  15.     return availability_mask;
  16. }
  17.  
  18. // main_module_entry_point.c
  19.  
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "print_module.h"
  23. #include "documentation_module.h"
  24.  
  25. int validate(char* data)
  26. {
  27.     int validation_result = (strcmp(data, Available_document) == 0);
  28.     return validation_result;
  29. }
  30.  
  31. int main()
  32. {
  33.     print_log(print_char, Module_load_successb);
  34.    
  35.     int availability_mask_count = Documents_count;
  36.     char **documents = Documents;
  37.     int *availability_mask = check_available_documentation_module(validate, availability_mask_count, documents);
  38.  
  39.     // Output availability for each document
  40.     for (int i = 0; i < availability_mask_count; i++) {
  41.         char *document = documents[i];
  42.         printf("%15s: %s\n", document, availability_mask[i] ? "available" : "unavailable");
  43.     }
  44.    
  45.     free(availability_mask);
  46.  
  47.     return 0;
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. //////////////////////////////////////////////////////////////////
  56.  
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59.  
  60. struct bstree_node {
  61.     int value;
  62.     struct bstree_node *left;
  63.     struct bstree_node *right;
  64. };
  65.  
  66. struct bstree_node *bstree_create_node(int value) {
  67.     struct bstree_node *node = (struct bstree_node *)malloc(sizeof(struct bstree_node));
  68.     node->value = value;
  69.     node->left = NULL;
  70.     node->right = NULL;
  71.     return node;
  72. }
  73.  
  74. void bstree_add_left(struct bstree_node *node, struct bstree_node *left) {
  75.     node->left = left;
  76. }
  77.  
  78. void bstree_add_right(struct bstree_node *node, struct bstree_node *right) {
  79.     node->right = right;
  80. }
  81.  
  82. void bstree_insert(struct bstree_node *root, struct bstree_node *node) {
  83.     if (root->value > node->value) {
  84.         if (root->left == NULL) {
  85.             root->left = node;
  86.         } else {
  87.             bstree_insert(root->left, node);
  88.         }
  89.     } else {
  90.         if (root->right == NULL) {
  91.             root->right = node;
  92.         } else {
  93.             bstree_insert(root->right, node);
  94.         }
  95.     }
  96. }
  97.  
  98. struct bstree_node *bstree_remove_node(struct bstree_node *root, int value) {
  99.     if (root == NULL) {
  100.         return NULL;
  101.     }
  102.  
  103.     if (value < root->value) {
  104.         root->left = bstree_remove_node(root->left, value);
  105.         return root;
  106.     } else if (value > root->value) {
  107.         root->right = bstree_remove_node(root->right, value);
  108.         return root;
  109.     } else {
  110.         if (root->left == NULL && root->right == NULL) {
  111.             free(root);
  112.             return NULL;
  113.         } else if (root->left == NULL) {
  114.             struct bstree_node *temp = root->right;
  115.             free(root);
  116.             return temp;
  117.         } else if (root->right == NULL) {
  118.             struct bstree_node *temp = root->left;
  119.             free(root);
  120.             return temp;
  121.         } else {
  122.             struct bstree_node *temp = root->right;
  123.             while (temp->left != NULL) {
  124.                 temp = temp->left;
  125.             }
  126.             root->value = temp->value;
  127.             root->right = bstree_remove_node(root->right, temp->value);
  128.             return root;
  129.         }
  130.     }
  131. }
  132.  
  133. void bstree_destroy(struct bstree_node *root) {
  134.     if (root == NULL) {
  135.         return;
  136.     }
  137.  
  138.     bstree_destroy(root->left);
  139.     bstree_destroy(root->right);
  140.     free(root);
  141. }
  142.  
  143.  
  144. void bstree_apply_infix(struct bstree_node *root, void (*func)(int)) {
  145.     if (root == NULL) {
  146.         return;
  147.     }
  148.  
  149.     bstree_apply_infix(root->left, func);
  150.     func(root->value);
  151.     bstree_apply_infix(root->right, func);
  152. }
  153.  
  154. void bstree_apply_prefix(struct bstree_node *root, void (*func)(int)) {
  155.     if (root == NULL) {
  156.         return;
  157.     }
  158.  
  159.     func(root->value);
  160.     bstree_apply_prefix(root->left, func);
  161.     bstree_apply_prefix(root->right, func);
  162. }
  163.  
  164. void bstree_apply_postfix(struct bstree_node *root, void (*func)(int)) {
  165.     if (root == NULL) {
  166.         return;
  167.     }
  168.  
  169.     bstree_apply_postfix(root->left, func);
  170.     bstree_apply_postfix(root->right, func);
  171.     func(root->value);
  172. }
  173.  
  174.  
  175.  
  176. // bst_create_test.c
  177. #include <stdio.h>
  178. #include <stdlib.h>
  179. #include "bstree.h"
  180.  
  181. int main(int argc, char *argv[]) {
  182.     struct bstree_node *root = bstree_create_node(50);
  183.     printf("Root node value: %d\n", root->value);
  184.     bstree_destroy(root);
  185.     return 0;
  186. }
  187.  
  188.  
  189. // bst_insert_test.c
  190. #include <stdio.h>
  191. #include <stdlib.h>
  192. #include "bstree.h"
  193.  
  194. int main(int argc, char *argv[]) {
  195.     struct bstree_node *root = bstree_create_node(50);
  196.     bstree_insert(root, 25);
  197.     bstree_insert(root, 75);
  198.     bstree_insert(root, 12);
  199.     bstree_insert(root, 37);
  200.     bstree_insert(root, 62);
  201.     bstree_insert(root, 87);
  202.     printf("In-order traversal:\n");
  203.     bstree_apply_infix(root, [](int value) {
  204.         printf("%d\n", value);
  205.     });
  206.     bstree_destroy(root);
  207.     return 0;
  208. }
  209.  
  210.  
  211. // bst_traverse_test.c
  212. #include <stdio.h>
  213. #include <stdlib.h>
  214. #include "bstree.h"
  215.  
  216. int main(int argc, char *argv[]) {
  217.     struct bstree_node *root = bstree_create_node(50);
  218.     bstree_insert(root, 25);
  219.     bstree_insert(root, 75);
  220.     bstree_insert(root, 12);
  221.     bstree_insert(root, 37);
  222.     bstree_insert(root, 62);
  223.     bstree_insert(root, 87);
  224.     printf("In-order traversal:\n");
  225.     bstree_apply_infix(root, [](int value) {
  226.         printf("%d\n", value);
  227.     });
  228.     printf("Pre-order traversal:\n");
  229.     bstree_apply_prefix(root, [](int value) {
  230.         printf("%d\n", value);
  231.     });
  232.     printf("Post-order traversal:\n");
  233.     bstree_apply_postfix(root, [](int value) {
  234.         printf("%d\n", value);
  235.     });
  236.     bstree_destroy(root);
  237.     return 0;
  238. }
  239.  
  240.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement