Also habe ich durch das K & R C Buch gelesen und habe eine Frage .. im 6. Kapitel auf structs auf Seite 140-141, gibt es Code, der wie folgt aussieht (habe ich einige der irrelevanten Teile aus)
/*
the program loops through a tree looking for some word
if it finds the word itll incremenet the count by 1
if it doesnt itll add a new node
*/
struct node {
char *word;
int count;
struct node *left;
struct node *right;
}
main() {
struct node *root;
char word[1000];
root = NULL;
while(getword(word, MAXWORD) != EOF) /* getword just grabs 1 word at a time from a file of words */
if(isalpha(word[0])) /* isalpha checks to see if it is a valid word */
root = addNode(root, word);
treeprint(root); /* prints the tree */
return 0;
}
struct node *addNode(struct node *p, char *w) {
int cond;
if(p == NULL) {
p = malloc(sizeof(struct node)); /* allocates memory for the new node */
p -> word = strdup(w);
p -> count = 1;
p -> left = p -> right = NULL;
}
else if ((cond = strcmp(w, p -> word)) == 0)
p -> count++;
else if(cond < 0)
p -> left = addNode(p -> left, w);
else
p -> right = addNode(p -> right, w);
return p;
}
Und meine Verwirrung ist in der main () Funktion bei root = addNode (root, word)
Wenn addNode einen Zeiger auf den neu hinzugefügten Knoten zurückgibt (oder an den Knoten dieses Wort ist, wenn seine bereits er Baum int), würde das nicht „verlieren“ alle Daten über dem Baum? Sollte nicht Aufenthalt als die Wurzel des Baumes Wurzel?
Vielen Dank!













