Initializacia
This commit is contained in:
parent
724940804f
commit
55853490b2
@ -13,47 +13,29 @@ 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)) {
|
char* r = fgets(buffer, SIZE, stdin);
|
||||||
buffer[strcspn(buffer, "\n")] = 0;
|
if (!r || buffer[0] == '\n') {
|
||||||
|
return NULL;
|
||||||
if (buffer[0] == '\0') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct tree* node = calloc(1, sizeof(struct tree));
|
|
||||||
strncpy(node->value, buffer, SIZE - 1);
|
|
||||||
return node;
|
|
||||||
}
|
}
|
||||||
|
buffer[strcspn(buffer, "\n")] = 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* 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();
|
||||||
|
|
||||||
if (!node->left || !node->right) {
|
|
||||||
destroy_tree(node);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void run_tree(struct tree* tree) {
|
void run_tree(struct tree* tree) {
|
||||||
if (!tree) {
|
if (!tree) {
|
||||||
return;
|
return;
|
||||||
@ -104,8 +86,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;
|
||||||
}
|
}
|
||||||
@ -116,8 +97,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