Initializacia

This commit is contained in:
Kozar 2024-11-12 12:50:45 +00:00
parent b701fb7fdc
commit 5501d3b44c

View File

@ -13,22 +13,27 @@ struct tree {
struct tree* read_tree() { struct tree* read_tree() {
char buffer[SIZE]; char buffer[SIZE];
memset(buffer, 0, SIZE); if (!fgets(buffer, SIZE, stdin)) {
char* r = fgets(buffer, SIZE, stdin);
if (!r || buffer[0] == '\n') {
return NULL; 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)); struct tree* node = calloc(1, sizeof(struct tree));
strncpy(node->value, buffer, SIZE - 1); strncpy(node->value, buffer, SIZE - 1);
return node; return node;
} }
struct tree* load_tree() { struct tree* load_tree() {
struct tree* node = read_tree(); struct tree* node = read_tree();
if (!node) { if (!node) {
return NULL; return NULL;
} }
if (node->value[0] != '*') { if (node->value[0] != '*') {
node->left = load_tree(); node->left = load_tree();
node->right = load_tree(); node->right = load_tree();
@ -38,10 +43,12 @@ struct tree* load_tree() {
return NULL; return NULL;
} }
} }
return node; return node;
} }
void run_tree(struct tree* tree) { void run_tree(struct tree* tree) {
if (!tree) { if (!tree) {
return; return;
@ -92,6 +99,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) {
printf("Chybna databaza\n"); printf("Chybna databaza\n");
return 0; return 0;
@ -103,7 +111,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;
} }