C ++访问结构中结构数组的元素

C++ access an element of struct array in a struct

这件事让我发疯了一段时间。

我需要创建并遍历(post 顺序)一棵通用树,其中每个节点(结构)都由用户通过控制台添加。
不允许使用STL。

用户指定将添加多少个节点,以及它可以容纳多少个 'child' 个节点(数字)和节点的名称(字符串)。 输入示例:

5
1个
2乙
1 C
1 D
3 E

上面的意思就是要增加5个节点。第一个 (A) 可以接受一个 'child' 节点,(B) 可以接受 2 个这样的节点,(C) 可以接受 1 个,依此类推。 新添加的节点必须始终添加到从顶部开始的 'highest' 可能的节点(如果它仍然可以接受新的 'child' 节点,如果不可能则转到下一个)。

想法是创建一个数组(我知道总共将添加多少个节点)并将用户指定的那些节点放在那里,然后'link'它们相应地使用结构内部的指针数组。

给定示例的输出应为:E C D B A
我把整个事情写成如下,但我无法遍历树:

结构:

struct node {
string name = "";
int noChildrenAdded = 0;
int possibleNoChildren = 0;
int childrenFreeSlots = 0;
node* children = nullptr;
node* father = nullptr;
};

遍历函数无效

void traverse(node* father)
{
cout << father->name << endl;
if (father == nullptr) {
    return;
}
for (int i = 0; i < father->possibleNoChildren; i++) {
    if (&father->children[i] == nullptr) {
        continue;
    }
    traverse(&father->children[i]);
}
cout << father->name << "\n";
}

主要

int main() {
int n = 0;
short g = 0;
string name;
cin >> n;
node* tree = new node[n];
node* tmp = nullptr;

//adding children to tree array
for (int i = 0; i < n; i++) {
    cin >> g >> name;
    tree[i].possibleNoChildren = tree[i].childrenFreeSlots = g;
    tree[i].name = name;
    tree[i].noChildrenAdded = 0;
    tree[i].children = new node[1];
}

// making connections between nodes
for (int son = 1; son < n; son++) {
    for (int father = 0; father < son; father++) {
        if (tree[father].childrenFreeSlots > 0) {

            //resizing array
            if (tree[father].noChildrenAdded == 0) {
                tree[father].children[0] = tree[son];
            }
            else {
                int added = tree[father].noChildrenAdded;
                tmp = new node[added + 1];
                for (int i = 0; i < added; i++) {
                    tmp[i] = tree[father].children[i];
                }
                delete[] tree[father].children;
                tree[father].children = nullptr;
                tree[father].children = tmp;
                tree[father].children[added] = tree[son];
                tmp = nullptr;
            }
            tree[father].noChildrenAdded++;
            tree[father].childrenFreeSlots -= 1;
            break;
        }
    }
}

//this is how it should be
cout << "Father: " << tree[1].name << "\tchildren added: " << tree[1].noChildrenAdded << endl;

//tree[0].children[0] is holding pointer to drzewo[1] so the below should give me the same answer as above.
//this is giving me wrong answer
node* ptr = &tree[0].children[0];
cout << "Father: " << ptr->name << "\tchildren added: " << ptr->noChildrenAdded << endl;

//traverse(&tree[0]);   
delete[] tree;

}

问题

我无法访问结构的详细信息(例如 noChildrenAdded)- 尽管填充了 noChildrenAdded,但我的结果为零。当我通过树数组访问它时,我得到了正确的数字,但是当我通过结构内部的指针访问它时,我得到了 0.

示例:
这是正确的:cout << "Father: " << tree[1].name << "\tchildren added: " << tree[1].noChildrenAdded << endl;

但这不是(尽管两者应该给出相同的number/answer):

//tree[0].children[0] is holding pointer to tree[1] so the below should give me the same answer as above.
    //this is giving me wrong answer
    node* ptr = &tree[0].children[0];
    cout << "Father: " << ptr->name << "\tchildren added: " << ptr->noChildrenAdded << endl;

我想我搞砸了将子项分配给结构内的 *children 数组。该名称似乎可以访问,但不是 noChildren。

两者应该给出相同的答案,但他们不是:

enter image description here

如有任何帮助,我们将不胜感激!

PS:当我将此代码与静态子数组一起使用时,一切正常,遍历工作正常,但当我获得动态数组时,它就坏了。静态数组行不通,因为它占用太多内存并且占用的时间太长,所以我的程序不符合要求。

正如@igor-tandetnik 所建议的那样,使用 node* 指针数组解决了这个问题。在我的例子中,解决方案是使用 node** children 而不是 node *children.