繁体   English   中英

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

[英]C++ access an element of struct array in a struct

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

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

用户指定将添加多少个节点,它可以容纳多少个“子”节点(编号)和节点名称(字符串)。 示例输入:

5
1 安
2 乙
1 C
1 天
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:当我将此代码与子项的静态数组一起使用时,一切正常,遍历工作正常,但是当我得到一个动态数组时,它就坏了。 静态数组唉不会这样做,因为它占用太多内存并且花费的时间太长,所以我的程序无法满足要求。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM