Advertisement
okpalan

programming-language.r1.c

May 14th, 2024 (edited)
648
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.25 KB | Fixit | 0 0
  1. ```h
  2. #ifndef TNARY_H
  3. #define TNARY_H
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdbool.h>
  9.  
  10. #define MARKER -1
  11.  
  12. typedef struct TnaryTreeNode_s TnaryTreeNode;
  13. struct TnaryTreeNode_s {
  14.     struct TnaryTreeNode_s **children;
  15.     char data[2];
  16. };
  17.  
  18. TnaryTreeNode *TnaryTreeNode_add_child(TnaryTreeNode *tree, TnaryTreeNode *node);
  19.  
  20. typedef struct TnaryTree_s TnaryTree;
  21. struct TnaryTree_s {
  22.     TnaryTreeNode *root;
  23. };
  24.  
  25. TnaryTree* TnaryTreeNode_new(char data[2]);
  26. void TnaryTree_free_node(TnaryTreeNode *node);
  27. void TnaryTree_serialize(FILE *fp, TnaryTree *tree);
  28. char* TnaryTree_deserialize(FILE *fp);
  29. void TnaryTree_printTree(TnaryTreeNode *node, int level);
  30.  
  31. #define MAP_MAX_HASH_SIZE  128
  32. typedef struct MapNode_s MapNode;
  33. struct MapNode_s {
  34.     char *data;
  35.     struct MapNode_s *next;
  36. };
  37.  
  38. typedef struct Map_s Map;
  39. struct Map_s {
  40.     char *key;
  41.     size_t count;
  42.     MapNode *value[MAP_MAX_HASH_SIZE];
  43. };
  44.  
  45. Map* Map_new();
  46. unsigned int Map_hash(char *str);
  47. bool Map_set(Map *map, char *key, MapNode *node);
  48. MapNode* Map_get(Map *map, char *key);
  49. void Map_clear(Map *map);
  50.  
  51. #endif /* TNARY_H */
  52. ```
  53. ```c
  54. #include "tnary.h"
  55. #include <stdio.h>
  56. #include <stdlib.h>
  57. #include <string.h>
  58. #include <stdbool.h>
  59.  
  60. // Implementation of TnaryTreeNode_new
  61. TnaryTree* TnaryTreeNode_new(char data[2]) {
  62.     TnaryTree *tree = (TnaryTree *)malloc(sizeof(TnaryTree));
  63.     TnaryTreeNode *node = (TnaryTreeNode *)malloc(sizeof(TnaryTreeNode));
  64.     node->children = (TnaryTreeNode **)malloc(3 * sizeof(TnaryTreeNode *));
  65.     for (int i = 0; i < 3; i++) {
  66.         node->children[i] = NULL;
  67.     }
  68.     strcpy(node->data, data);
  69.     tree->root = node;
  70.     return tree;
  71. }
  72.  
  73. // Implementation of TnaryTree_free_node
  74. void TnaryTree_free_node(TnaryTreeNode *node) {
  75.     for (int i = 0; i < 3; i++) {
  76.         if (node->children[i] != NULL) {
  77.             TnaryTree_free_node(node->children[i]);
  78.         }
  79.     }
  80.     free(node->children);
  81.     free(node);
  82. }
  83. TnaryTreeNode *TnaryTreeNode_add_child(TnaryTreeNode *parent, TnaryTreeNode *child) {
  84.     for (int i = 0; i < 3; i++) {
  85.         if (parent->children[i] == NULL) {
  86.             parent->children[i] = child;
  87.             return child;
  88.         }
  89.     }
  90.     return NULL;
  91. }
  92.  
  93.  
  94.  
  95. // Implementation of TnaryTree_serialize
  96. void TnaryTree_serialize(FILE *fp, TnaryTree *tree) {
  97.     for (int i = 0; i < 3; i++) {
  98.         if (tree->root->children[i] != NULL) {
  99.             fprintf(fp, "%s", tree->root->children[i]->data);
  100.         }
  101.     }
  102. }
  103.  
  104. // Implementation of TnaryTree_deserialize
  105. char* TnaryTree_deserialize(FILE *fp) {
  106.     char *data = (char *)malloc(sizeof(char) * 2);
  107.     fscanf(fp, "%s", data);
  108.     for (int i = 0; i < 3; i++) {
  109.         TnaryTreeNode *node = (TnaryTreeNode *)malloc(sizeof(TnaryTreeNode));
  110.         node->children = (TnaryTreeNode **)malloc(3 * sizeof(TnaryTreeNode *));
  111.         for (int i = 0; i < 3; i++) {
  112.             node->children[i] = NULL;
  113.         }
  114.         fscanf(fp, "%s", node->data);
  115.  
  116.      TnaryTree_printTree(node, 0);
  117.     }
  118.     return data;
  119. }
  120.  
  121. void TnaryTree_printTree(TnaryTreeNode *node, int level) {
  122.     if (node == NULL) {
  123.         return;
  124.     }
  125.     for (int i = 0; i < level; i++) {
  126.         printf("| ");
  127.     }
  128.     printf("%s\n", node->data);
  129.     for (int i = 0; i < 3; i++) {
  130.         TnaryTree_printTree(node->children[i], level + 1);
  131.     }
  132. }
  133.  
  134. // Implementation of Map_new
  135. Map* Map_new() {
  136.     Map *map = (Map *)malloc(sizeof(Map));
  137.     map->key = NULL;
  138.     map->count = 0;
  139.     for (int i = 0; i < MAP_MAX_HASH_SIZE; i++) {
  140.         map->value[i] = NULL;
  141.     }
  142.     return map;
  143. }
  144.  
  145. // Implementation of Map_hash
  146. unsigned int Map_hash(char *str) {
  147.     unsigned int hash = 5381;
  148.     int c;
  149.     while ((c = *str++)) {
  150.         hash = ((hash << 5) + hash) + c;
  151.     }
  152.     return hash % MAP_MAX_HASH_SIZE;
  153. }
  154.  
  155. // Implementation of Map_set
  156. bool Map_set(Map *map, char *key, MapNode *new_node) {
  157.     unsigned int hash = Map_hash(key);
  158.     MapNode *node = map->value[hash];
  159.     if (node == NULL) {
  160.         map->value[hash] = new_node;
  161.         map->count++;
  162.         return true;
  163.     }
  164.     while (node->next != NULL) {
  165.         node = node->next;
  166.     }
  167.     node->next = new_node;
  168.     map->count++;
  169.     return true;
  170. }
  171.  
  172. // Implementation of Map_get
  173. MapNode* Map_get(Map *map, char *key) {
  174.     unsigned int hash = Map_hash(key);
  175.     MapNode *node = map->value[hash];
  176.     while (node != NULL) {
  177.         if (strcmp(node->data, key) == 0) {
  178.             return node;
  179.         }
  180.         node = node->next;
  181.     }
  182.     return NULL;
  183. }
  184.  
  185. // Implementation of Map_clear
  186. void Map_clear(Map *map) {
  187.     for (int i = 0; i < MAP_MAX_HASH_SIZE; i++) {
  188.         MapNode *node = map->value[i];
  189.         while (node != NULL) {
  190.             MapNode *temp = node;
  191.             node = node->next;
  192.             free(temp);
  193.         }
  194.     }
  195.     free(map);
  196. }
  197. ```
  198. ```c
  199. // main.c
  200. #include <stdio.h>
  201. #include <stdlib.h>
  202. #include <string.h>
  203. #include <stdbool.h>
  204. #include "tnary.h"
  205.  
  206. int main() {
  207.     TnaryTree *tree_node = TnaryTreeNode_new("a");
  208.     TnaryTree *node = TnaryTreeNode_new("b");
  209.     TnaryTreeNode_add_child(tree_node->root, node->root);
  210.     node = TnaryTreeNode_new("c");
  211.     TnaryTreeNode_add_child(tree_node->root, node->root);
  212.     node = TnaryTreeNode_new("d");
  213.     TnaryTreeNode_add_child(tree_node->root, node->root);
  214.  
  215.     FILE *fp = fopen("test.txt", "w");
  216.     TnaryTree_serialize(fp, tree_node);
  217.     fclose(fp);
  218.  
  219.     fp = fopen("test.txt", "r");
  220.     char *data = TnaryTree_deserialize(fp);
  221.     printf("Deserialized data: %s\n", data);
  222.     free(data);
  223.     fclose(fp);
  224.    
  225.     TnaryTree_free_node(tree_node->root);
  226.  
  227.     Map *map = Map_new();
  228.    
  229.     // Set data of tree_node root to contain characters within the node and add it to the Map
  230.     MapNode *new_node_a = (MapNode *)malloc(sizeof(MapNode));
  231.     new_node_a->data = "a"; // Changed 'key' to 'data' according to the typedef
  232.     new_node_a->next = NULL;
  233.     Map_set(map, "fn", new_node_a); // Function keyword
  234.  
  235.     MapNode *new_node_b = (MapNode *)malloc(sizeof(MapNode));
  236.     new_node_b->data = "b"; // Changed 'key' to 'data' according to the typedef
  237.     new_node_b->next = NULL;
  238.     Map_set(map, "b", new_node_b);
  239.  
  240.     MapNode *new_node_c = (MapNode *)malloc(sizeof(MapNode));
  241.     new_node_c->data = "c"; // Changed 'key' to 'data' according to the typedef
  242.     new_node_c->next = NULL;
  243.     Map_set(map, "c", new_node_c);
  244.  
  245.     MapNode *new_node_d = (MapNode *)malloc(sizeof(MapNode));
  246.     new_node_d->data = "d"; // Changed 'key' to 'data' according to the typedef
  247.     new_node_d->next = NULL;
  248.     Map_set(map, "d", new_node_d);
  249.  
  250.     // Retrieving and printing data from the map
  251.     printf("Data in the map:\n");
  252.    MapNode *map_node = Map_get(map, "fn"); // Changed the key to "fn"
  253. printf("%s\n", map_node->data); // Changed 'key' to 'data' according to the typedef
  254.  
  255. for(size_t i = 0; i < MAP_MAX_HASH_SIZE; i++) {
  256.     if(map->value[i] != NULL) { // Changed 'buckets' to 'value' according to the typedef
  257.         printf("%s\n", map->value[i]->data); // Changed 'key' to 'data' according to the typedef
  258.     }
  259. }
  260.  
  261.  
  262.     // Freeing memory
  263.     Map_clear(map);
  264.  
  265.     return 0;
  266. }
  267. // gcc -Wall -Werror -ansi -Wpedantic -g main.c tnary.c tnary.h -o tnary -std=c99
  268.  
  269. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement