[英]Binary Tree Struct containing Multiple Arrays
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. 本质上,我需要创建一个树,该树在每个Node中包含两个数据数组和两个单独的char数组。
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, ie getchar
until .
作为终止,现在,我有一个理论来解析它,即
getchar
直到.
and scanf
the name
and number
into an array. 并将
name
和number
scanf
到一个数组中。 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 (ie the last character is '\\0'), you can use the strlen
function to get their size, and pass that value to malloc
in order to allocate the memory before you use strcpy
to copy the string into that memory. 如果您读取的
char*
以空字符结尾(即最后一个字符为'\\ 0'),则可以使用strlen
函数获取其大小,然后将该值传递给malloc
以便在使用strcpy
之前分配内存将字符串复制到该内存中。 Don't forget to call free
to return everything you malloc
'ed 别忘了
free
调用以返回您malloc
的所有内容
So 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) 因此,只需将两个
char*
传递给将它们插入您的数据结构中的函数(是二叉树还是try?您在标题中使用了一个术语,而在问题中使用了另一个术语)
searched google for "c binary tree tutorial": 在Google中搜索“ C二叉树教程”:
http://www.cprogramming.com/tutorial/c/lesson18.html http://www.cprogramming.com/tutorial/c/lesson18.html
I would do a stack type: 我会做一个堆栈类型:
typedef struct
{
int pos;
char *array;
int size;
} charArray;
charArray *newCharArray();
void pushCharArray(charArray * ca, char c);
void popCharArray(charArray *ca);
charStack *name;
charStack *number;
Inside pushCharArray you should check if ca is null or not, increment size if you need more space, increment the position... 在pushCharArray内部,您应该检查ca是否为null,如果需要更多空间,则增加大小,增加位置...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.