Я создал древовидную структуру, прочитал слова из файла и зарегистрировал их в дереве. Но enter()
не работает должным образом. Я отлаживал с помощью gdb и устанавливал линию 42 точки останова. Затем я ввел print *node
print *root
.двоичный код дерева не работает должным образом
(gdb) print node
$9 = (struct node *) 0x603250
(gdb) print *node
$10 = {left = 0x0, right = 0x0, word = 0x0}
(gdb) print root
$11 = (struct node *) 0x0
(gdb) print *root
Cannot access memory at address 0x0
Почему root
ничего не говорит? Почему не node->word
Точка в виде данного слова?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct node {
struct node *left;
struct node *right;
char *word;
};
static struct node *root = NULL;
void memory_error(void) {
fprintf(stderr, "Error: Out of memory\n");
exit(8);
}
char *save_string(char *string) {
char *new_string;
new_string = malloc((unsigned) (strlen(string) + 1));
if (new_string == NULL)
memory_error();
strcpy(new_string, string);
return (new_string);
}
void enter(struct node *node, char *word) {
int result;
if (node == NULL) {
node = malloc(sizeof(struct node));
if (node == NULL)
memory_error();
node->left = NULL;
node->right = NULL;
node->word = save_string(word);
return;
}
result = strcmp(node->word, word);
if (result == 0)
return;
if (result < 0)
enter(node->right, word);
else
enter(node->left, word);
}
void scan(char *name) {
char word[100];
int index;
int ch;
FILE *in_file;
in_file = fopen(name, "r");
if (in_file == NULL) {
fprintf(stderr, "Error: Unable to open %s\n", name);
exit(8);
}
while (1) {
while (1) {
ch = fgetc(in_file);
if (isalpha(ch) || (ch == EOF))
break;
}
if (ch == EOF)
break;
word[0] = ch;
for (index = 1; index < sizeof(word); index++) {
ch = fgetc(in_file);
if (!isalpha(ch))
break;
word[index] = ch;
}
word[index] = '\0';
enter(root, word);
}
fclose(in_file);
}
void print_tree(struct node *top) {
if (top == NULL)
return;
print_tree(top->left);
printf("%s\n", top->word);
print_tree(top->right);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Error: Wrong number of parameters\n");
fprintf(stderr, " on the command line\n");
fprintf(stderr, "Usage is:\n");
fprintf(stderr, " words 'file'\n");
exit(8);
}
scan(argv[1]);
print_tree(root);
return (0);
}
Я передал этой программе следующий текст в качестве аргумента «a.txt».
apple
orange
lemon
banana
pine
stroberry
ruby
perl
python
c
Альтернативно, если по какой-то причине вы не хотите возвращать 'root' (потому что, скажем, вы хотите вернуть код ошибки), вы можете передать указатель на' root', чтобы вы могли назначьте 'root' внутри функции. – Thomas