Just as a disclaimer, I am not looking for any hard code solutions, but merely a nudge in the right direction.
Essentially, I need to create a tree, that contains two arrays of data in each Node and two separate arrays of chars.
struct Node {
char *name;
char *number;
struct Node *left;
struct Node *left;
};
That’s my struct at the moment, and the input is in the form:
name number
name number
name number
.
The . being the termination, now, I have a theory for how to parse that, i.e. getchar until . and scanf the name and number into an array. But from this point, I’m unsure how exactly I need to pass those arrays to a function to add the stuff to the tree, where I define the size of the array, etc. Can someone give some tips for this problem?
First of all, you need to use dynamic memory. The size of your arrays will be defined at runtime, after you have read them, from a file I guess.
If the
char*you read are null-terminated (i.e. the last character is ‘\0’), you can use thestrlenfunction to get their size, and pass that value tomallocin order to allocate the memory before you usestrcpyto copy the string into that memory. Don’t forget to callfreeto return everything youmalloc‘edSo just pass two
char*to the function which will insert them in your data structure (is it a binary tree or a try? You used one term in the title and another in your question)