diff --git a/cv7/program.c b/cv7/program.c index 5b330c1..9b3f929 100644 --- a/cv7/program.c +++ b/cv7/program.c @@ -13,23 +13,28 @@ struct tree { struct tree* read_tree() { char buffer[SIZE]; - memset(buffer, 0, SIZE); - char* r = fgets(buffer, SIZE, stdin); - if (!r || buffer[0] == '\n') { + if (!fgets(buffer, SIZE, stdin)) { return NULL; } - buffer[strcspn(buffer, "\n")] = 0; + buffer[strcspn(buffer, "\n")] = 0; + + if (buffer[0] == '\0') { + return NULL; + } + struct tree* node = calloc(1, sizeof(struct tree)); strncpy(node->value, buffer, SIZE - 1); return node; } + struct tree* load_tree() { struct tree* node = read_tree(); if (!node) { return NULL; } - if (node->value[0] != '*') { + + if (node->value[0] != '*') { node->left = load_tree(); node->right = load_tree(); @@ -38,10 +43,12 @@ struct tree* load_tree() { return NULL; } } + return node; } + void run_tree(struct tree* tree) { if (!tree) { return; @@ -92,7 +99,8 @@ void count_items(struct tree* tree, int* count) { int main() { struct tree* root = load_tree(); printf("Expert z bufetu to vie.\n"); - if (!root) { + + if (!root) { printf("Chybna databaza\n"); return 0; } @@ -103,7 +111,7 @@ int main() { printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n"); run_tree(root); - destroy_tree(root); return 0; } +