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

eb

这件事使我疯狂了一段时间了。

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

用户指定要添加的节点数,可以容纳的“子”节点数(数字)和节点名称(字符串)。输入示例:

5
1 A
2 B
1 C
1 D
3 E

以上意味着将添加5个节点。第一个(A)可以接受一个“子”节点,(B)可以接受2个这样的节点,而(C)可以接受1个,等等。新添加的节点必须始终从顶部添加到“最高”节点(如果它仍然可以接受一个新的“子”节点,那么如果不能,则转到下一个)。

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

给定示例的输出应为:ECDBA
我将整个内容编写如下,但是无法遍历树:

结构体:

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;

但这不是(尽管两者应该给出相同的数字/答案):

//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则不能。

两者应该给出相同的答案,但它们却不一样:

在此处输入图片说明

任何帮助将不胜感激!

PS:当我将此代码与子级静态数组一起使用时,一切正常,遍历可以正常工作,但是当我得到动态数组时,遍历就坏了。静态数组a不会这样做,因为它占用过多的内存,并且花费的时间太长,因此我的程序无法满足要求。

eb

就像@ igor-tandetnik所建议的那样,使用node *指针数组解决了该问题。就我而言,解决方案是使用node** childrennot node *children

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章