Initializacia
This commit is contained in:
parent
5001be183c
commit
6ec70db799
@ -1,6 +1,5 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define SIZE 100
|
#define SIZE 100
|
||||||
@ -13,24 +12,20 @@ struct tree {
|
|||||||
|
|
||||||
struct tree* read_tree() {
|
struct tree* read_tree() {
|
||||||
char buffer[SIZE];
|
char buffer[SIZE];
|
||||||
|
memset(buffer, 0, SIZE);
|
||||||
|
|
||||||
while (fgets(buffer, SIZE, stdin)) {
|
if (!fgets(buffer, SIZE, stdin) || buffer[0] == '\n') {
|
||||||
buffer[strcspn(buffer, "\n")] = 0;
|
return NULL;
|
||||||
|
|
||||||
if (buffer[0] == '\0') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct tree* node = calloc(1, sizeof(struct tree));
|
|
||||||
strncpy(node->value, buffer, SIZE - 1);
|
|
||||||
return node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
buffer[strcspn(buffer, "\n")] = 0;
|
||||||
|
|
||||||
|
struct tree* node = (struct tree*)calloc(1, sizeof(struct tree));
|
||||||
|
strncpy(node->value, buffer, SIZE - 1);
|
||||||
|
|
||||||
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
struct tree* load_tree() {
|
struct tree* load_tree() {
|
||||||
struct tree* node = read_tree();
|
struct tree* node = read_tree();
|
||||||
if (!node) {
|
if (!node) {
|
||||||
@ -40,25 +35,18 @@ struct tree* load_tree() {
|
|||||||
if (node->value[0] != '*') {
|
if (node->value[0] != '*') {
|
||||||
node->left = load_tree();
|
node->left = load_tree();
|
||||||
node->right = load_tree();
|
node->right = load_tree();
|
||||||
} else {
|
|
||||||
node->left = NULL;
|
|
||||||
node->right = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void run_tree(struct tree* tree) {
|
void run_tree(struct tree* tree) {
|
||||||
if (!tree) {
|
if (!tree) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tree->value[0] == '*') {
|
if (tree->value[0] == '*') {
|
||||||
printf("*%s\nKoniec\n", tree->value + 1);
|
printf("%s\nKoniec\n", tree->value + 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,16 +69,20 @@ void run_tree(struct tree* tree) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void destroy_tree(struct tree* tree) {
|
void destroy_tree(struct tree* tree) {
|
||||||
if (tree->left != NULL) {
|
if (!tree) {
|
||||||
destroy_tree(tree->left);
|
return;
|
||||||
}
|
|
||||||
if (tree->right != NULL) {
|
|
||||||
destroy_tree(tree->right);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
destroy_tree(tree->left);
|
||||||
|
destroy_tree(tree->right);
|
||||||
free(tree);
|
free(tree);
|
||||||
}
|
}
|
||||||
|
|
||||||
void count_items(struct tree* tree, int* count) {
|
void count_items(struct tree* tree, int* count) {
|
||||||
|
if (!tree) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (tree->left == NULL && tree->right == NULL) {
|
if (tree->left == NULL && tree->right == NULL) {
|
||||||
(*count)++;
|
(*count)++;
|
||||||
} else {
|
} else {
|
||||||
@ -101,6 +93,7 @@ void count_items(struct tree* tree, int* count) {
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
struct tree* root = load_tree();
|
struct tree* root = load_tree();
|
||||||
|
|
||||||
printf("Expert z bufetu to vie.\n");
|
printf("Expert z bufetu to vie.\n");
|
||||||
|
|
||||||
if (!root) {
|
if (!root) {
|
||||||
@ -114,8 +107,7 @@ int main() {
|
|||||||
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
printf("Odpovedajte 'a' pre prvu moznost alebo 'n' pre druhu moznost.\n");
|
||||||
|
|
||||||
run_tree(root);
|
run_tree(root);
|
||||||
|
|
||||||
destroy_tree(root);
|
destroy_tree(root);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user