Initializacia
This commit is contained in:
parent
57556018ea
commit
8109d836ab
@ -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
|
||||||
@ -11,6 +10,8 @@ struct tree {
|
|||||||
struct tree* right;
|
struct tree* right;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int error_flag = 0;
|
||||||
|
|
||||||
struct tree* read_tree() {
|
struct tree* read_tree() {
|
||||||
char buffer[SIZE];
|
char buffer[SIZE];
|
||||||
memset(buffer, 0, SIZE);
|
memset(buffer, 0, SIZE);
|
||||||
@ -20,6 +21,11 @@ struct tree* read_tree() {
|
|||||||
}
|
}
|
||||||
buffer[strcspn(buffer, "\n")] = 0;
|
buffer[strcspn(buffer, "\n")] = 0;
|
||||||
struct tree* node = calloc(1, sizeof(struct tree));
|
struct tree* node = calloc(1, sizeof(struct tree));
|
||||||
|
if (buffer[0] == '*') {
|
||||||
|
if (strlen(buffer) <= 1) {
|
||||||
|
error_flag = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
strncpy(node->value, buffer, SIZE - 1);
|
strncpy(node->value, buffer, SIZE - 1);
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@ -29,7 +35,7 @@ struct tree* load_tree() {
|
|||||||
if (!node) {
|
if (!node) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (node->value[0] != '*') {
|
if (error_flag || node->value[0] != '*') {
|
||||||
node->left = load_tree();
|
node->left = load_tree();
|
||||||
node->right = load_tree();
|
node->right = load_tree();
|
||||||
}
|
}
|
||||||
@ -65,16 +71,18 @@ 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 {
|
||||||
@ -86,8 +94,9 @@ 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 || error_flag) {
|
||||||
printf("Chybna databaza\n");
|
printf("Chybna databaza\n");
|
||||||
|
destroy_tree(root);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user