Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```h
- #ifndef TNARY_H
- #define TNARY_H
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #define MARKER -1
- typedef struct TnaryTreeNode_s TnaryTreeNode;
- struct TnaryTreeNode_s {
- struct TnaryTreeNode_s **children;
- char data[2];
- };
- TnaryTreeNode *TnaryTreeNode_add_child(TnaryTreeNode *tree, TnaryTreeNode *node);
- typedef struct TnaryTree_s TnaryTree;
- struct TnaryTree_s {
- TnaryTreeNode *root;
- };
- TnaryTree* TnaryTreeNode_new(char data[2]);
- void TnaryTree_free_node(TnaryTreeNode *node);
- void TnaryTree_serialize(FILE *fp, TnaryTree *tree);
- char* TnaryTree_deserialize(FILE *fp);
- void TnaryTree_printTree(TnaryTreeNode *node, int level);
- #define MAP_MAX_HASH_SIZE 128
- typedef struct MapNode_s MapNode;
- struct MapNode_s {
- char *data;
- struct MapNode_s *next;
- };
- typedef struct Map_s Map;
- struct Map_s {
- char *key;
- size_t count;
- MapNode *value[MAP_MAX_HASH_SIZE];
- };
- Map* Map_new();
- unsigned int Map_hash(char *str);
- bool Map_set(Map *map, char *key, MapNode *node);
- MapNode* Map_get(Map *map, char *key);
- void Map_clear(Map *map);
- #endif /* TNARY_H */
- ```
- ```c
- #include "tnary.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- // Implementation of TnaryTreeNode_new
- TnaryTree* TnaryTreeNode_new(char data[2]) {
- TnaryTree *tree = (TnaryTree *)malloc(sizeof(TnaryTree));
- TnaryTreeNode *node = (TnaryTreeNode *)malloc(sizeof(TnaryTreeNode));
- node->children = (TnaryTreeNode **)malloc(3 * sizeof(TnaryTreeNode *));
- for (int i = 0; i < 3; i++) {
- node->children[i] = NULL;
- }
- strcpy(node->data, data);
- tree->root = node;
- return tree;
- }
- // Implementation of TnaryTree_free_node
- void TnaryTree_free_node(TnaryTreeNode *node) {
- for (int i = 0; i < 3; i++) {
- if (node->children[i] != NULL) {
- TnaryTree_free_node(node->children[i]);
- }
- }
- free(node->children);
- free(node);
- }
- TnaryTreeNode *TnaryTreeNode_add_child(TnaryTreeNode *parent, TnaryTreeNode *child) {
- for (int i = 0; i < 3; i++) {
- if (parent->children[i] == NULL) {
- parent->children[i] = child;
- return child;
- }
- }
- return NULL;
- }
- // Implementation of TnaryTree_serialize
- void TnaryTree_serialize(FILE *fp, TnaryTree *tree) {
- for (int i = 0; i < 3; i++) {
- if (tree->root->children[i] != NULL) {
- fprintf(fp, "%s", tree->root->children[i]->data);
- }
- }
- }
- // Implementation of TnaryTree_deserialize
- char* TnaryTree_deserialize(FILE *fp) {
- char *data = (char *)malloc(sizeof(char) * 2);
- fscanf(fp, "%s", data);
- for (int i = 0; i < 3; i++) {
- TnaryTreeNode *node = (TnaryTreeNode *)malloc(sizeof(TnaryTreeNode));
- node->children = (TnaryTreeNode **)malloc(3 * sizeof(TnaryTreeNode *));
- for (int i = 0; i < 3; i++) {
- node->children[i] = NULL;
- }
- fscanf(fp, "%s", node->data);
- TnaryTree_printTree(node, 0);
- }
- return data;
- }
- void TnaryTree_printTree(TnaryTreeNode *node, int level) {
- if (node == NULL) {
- return;
- }
- for (int i = 0; i < level; i++) {
- printf("| ");
- }
- printf("%s\n", node->data);
- for (int i = 0; i < 3; i++) {
- TnaryTree_printTree(node->children[i], level + 1);
- }
- }
- // Implementation of Map_new
- Map* Map_new() {
- Map *map = (Map *)malloc(sizeof(Map));
- map->key = NULL;
- map->count = 0;
- for (int i = 0; i < MAP_MAX_HASH_SIZE; i++) {
- map->value[i] = NULL;
- }
- return map;
- }
- // Implementation of Map_hash
- unsigned int Map_hash(char *str) {
- unsigned int hash = 5381;
- int c;
- while ((c = *str++)) {
- hash = ((hash << 5) + hash) + c;
- }
- return hash % MAP_MAX_HASH_SIZE;
- }
- // Implementation of Map_set
- bool Map_set(Map *map, char *key, MapNode *new_node) {
- unsigned int hash = Map_hash(key);
- MapNode *node = map->value[hash];
- if (node == NULL) {
- map->value[hash] = new_node;
- map->count++;
- return true;
- }
- while (node->next != NULL) {
- node = node->next;
- }
- node->next = new_node;
- map->count++;
- return true;
- }
- // Implementation of Map_get
- MapNode* Map_get(Map *map, char *key) {
- unsigned int hash = Map_hash(key);
- MapNode *node = map->value[hash];
- while (node != NULL) {
- if (strcmp(node->data, key) == 0) {
- return node;
- }
- node = node->next;
- }
- return NULL;
- }
- // Implementation of Map_clear
- void Map_clear(Map *map) {
- for (int i = 0; i < MAP_MAX_HASH_SIZE; i++) {
- MapNode *node = map->value[i];
- while (node != NULL) {
- MapNode *temp = node;
- node = node->next;
- free(temp);
- }
- }
- free(map);
- }
- ```
- ```c
- // main.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include "tnary.h"
- int main() {
- TnaryTree *tree_node = TnaryTreeNode_new("a");
- TnaryTree *node = TnaryTreeNode_new("b");
- TnaryTreeNode_add_child(tree_node->root, node->root);
- node = TnaryTreeNode_new("c");
- TnaryTreeNode_add_child(tree_node->root, node->root);
- node = TnaryTreeNode_new("d");
- TnaryTreeNode_add_child(tree_node->root, node->root);
- FILE *fp = fopen("test.txt", "w");
- TnaryTree_serialize(fp, tree_node);
- fclose(fp);
- fp = fopen("test.txt", "r");
- char *data = TnaryTree_deserialize(fp);
- printf("Deserialized data: %s\n", data);
- free(data);
- fclose(fp);
- TnaryTree_free_node(tree_node->root);
- Map *map = Map_new();
- // Set data of tree_node root to contain characters within the node and add it to the Map
- MapNode *new_node_a = (MapNode *)malloc(sizeof(MapNode));
- new_node_a->data = "a"; // Changed 'key' to 'data' according to the typedef
- new_node_a->next = NULL;
- Map_set(map, "fn", new_node_a); // Function keyword
- MapNode *new_node_b = (MapNode *)malloc(sizeof(MapNode));
- new_node_b->data = "b"; // Changed 'key' to 'data' according to the typedef
- new_node_b->next = NULL;
- Map_set(map, "b", new_node_b);
- MapNode *new_node_c = (MapNode *)malloc(sizeof(MapNode));
- new_node_c->data = "c"; // Changed 'key' to 'data' according to the typedef
- new_node_c->next = NULL;
- Map_set(map, "c", new_node_c);
- MapNode *new_node_d = (MapNode *)malloc(sizeof(MapNode));
- new_node_d->data = "d"; // Changed 'key' to 'data' according to the typedef
- new_node_d->next = NULL;
- Map_set(map, "d", new_node_d);
- // Retrieving and printing data from the map
- printf("Data in the map:\n");
- MapNode *map_node = Map_get(map, "fn"); // Changed the key to "fn"
- printf("%s\n", map_node->data); // Changed 'key' to 'data' according to the typedef
- for(size_t i = 0; i < MAP_MAX_HASH_SIZE; i++) {
- if(map->value[i] != NULL) { // Changed 'buckets' to 'value' according to the typedef
- printf("%s\n", map->value[i]->data); // Changed 'key' to 'data' according to the typedef
- }
- }
- // Freeing memory
- Map_clear(map);
- return 0;
- }
- // gcc -Wall -Werror -ansi -Wpedantic -g main.c tnary.c tnary.h -o tnary -std=c99
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement