Initializacia

This commit is contained in:
Kozar 2024-11-12 12:56:52 +00:00
parent ee9e324e9f
commit cf685571c5

View File

@ -13,21 +13,24 @@ struct tree {
struct tree* read_tree() { struct tree* read_tree() {
char buffer[SIZE]; char buffer[SIZE];
if (!fgets(buffer, SIZE, stdin)) {
return NULL; while (fgets(buffer, SIZE, stdin)) {
}
buffer[strcspn(buffer, "\n")] = 0; buffer[strcspn(buffer, "\n")] = 0;
if (buffer[0] == '\0') { if (buffer[0] == '\0') {
return NULL; continue;
} }
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;
}
return NULL;
} }
struct tree* load_tree() { struct tree* load_tree() {
struct tree* node = read_tree(); struct tree* node = read_tree();
if (!node) { if (!node) {
@ -42,13 +45,6 @@ struct tree* load_tree() {
destroy_tree(node); destroy_tree(node);
return NULL; return NULL;
} }
} else {
struct tree* extra = read_tree();
if (extra) {
free(extra);
destroy_tree(node);
return NULL;
}
} }
return node; return node;
@ -57,6 +53,7 @@ struct tree* load_tree() {
void run_tree(struct tree* tree) { void run_tree(struct tree* tree) {
if (!tree) { if (!tree) {
return; return;