【问题标题】:Problem with accessing/assigning values in a struct in a list - C++访问/分配列表中结构中的值的问题 - C++
【发布时间】:2010-02-06 07:58:36
【问题描述】:

我正在尝试制作一个 Trie 数据结构(学校作业),并且我正在使用一个我自己也制作的列表并且工作正常(经过测试)来存储 Trie 中的 N 节点。

无论如何,问题是,每个节点都应该存储一个节点列表,这样我就可以制作我的 N-ary 树/trie,但我遇到了一个障碍......

当我调试并通过 for 循环时,我可以看到 currentNode 越来越深入到我的树中。但是,当我从根的角度来看它时,我的根只有一个链表,其中包含在循环的第一次迭代中创建的第一个节点。连续迭代不会在根节点的分支中的节点中注册......但它在 currentNode 中工作,就好像它们是单独的副本一样,即使 currentNode 是指向正确(我希望)节点的指针。

我的代码有问题吗?!?我是否误解了指针的工作原理?帮助!谢谢。

我的节点结构。

struct Node {
    char         letter;
    ItemType     item;
    List<Node> * branches;
};

Node * root;
int    size;

我的 put 函数。

ItemType put(KeyType newKey, ItemType newItem) {
    Node * currentNode = root;
    if (isEmpty()) {

        //So build a new key
        for (int levelIndex = 1; ((int) (newKey.length()) - levelIndex) >= 0; ++levelIndex) {
            currentNode->branches = new List<Node>;
            Node * tempNode = new Node;
            tempNode->letter = newKey.at(levelIndex - 1);
            currentNode->branches->add(*tempNode);
            currentNode = tempNode;
        }
        //Store
        currentNode->item = newItem;
        ++size;
        return NULL; //The former item.

    } else {
        //Begin
        return puttanesca(newKey, newItem, *(currentNode->branches), 1, 1);
    }
}

编辑:哦,抱歉,我忘了说 puttanesca 是一个递归函数,我用它来遍历和放置节点和东西。但我还没有真正测试过,因为这个问题,我被困在这里只是想将第一个键添加到我的空 Trie 中......

更多编辑:

这里是 puttanesca,虽然我不认为它与问题有关,但是......反正就是这样。

我正在将 Node 结构中的 List 指针更改为只是一个对象,因此其中一些内容可能看起来有问题,或者一开始可能就是错误的,因为我不太擅长 C++,而且我'这里仍然有问题,但可以看到一般概念/算法......哦,我使用 typedef string KeyType 作为我的密钥,只是为了一些未来的校对和一个 模板 表示 ItemType。

ItemType puttanesca(KeyType newKey, ItemType newItem, List<Node> & tempList, int listIndex, int levelIndex) {
    Node currentNode = tempList.get(listIndex);

    //Am I at the right node? (searching)
    if (newKey.at(levelIndex - 1) == currentNode.letter) { //Yes, I am.
        //Is this a leaf node?
        if (currentNode.branches == NULL) {

            //Key does not already exist
            if (newKey.length() != levelIndex) {
                //So build a new key
                for (; ((int) (newKey.length()) - levelIndex) >= 0; ++levelIndex) {
                    currentNode.branches = new List<Node>;
                    Node * tempNode = new Node;
                    tempNode->letter = newKey.at(levelIndex - 1);
                    currentNode.branches.add(*tempNode);
                    currentNode = *tempNode;
                }
                //Store
                currentNode.item = newItem;
                ++size;
                return NULL; //The former item.

            } else { //Key matched!
                //Replace with new item
                ItemType currentItem = currentNode.item;
                currentNode.item = newItem;
                return currentItem; //which is actually the now old item after the previous statement
            }
        } else { //Not a leaf, keep going
            //Go to the next level and start from the first index
            ItemType currentItem = puttanesca(newKey, newItem, currentNode.branches, 1, levelIndex + 1);
            if (currentItem == NULL) {
                //Key found to be inexistant
                //So build a new key - create new sibling
                Node * tempNode = new Node;
                tempNode->letter = newKey.at(levelIndex - 1);
                currentNode.branches.add(*tempNode);
                currentNode = *tempNode;

                //Continue building key - extend sibling
                for (++levelIndex; ((int) (newKey.length()) - levelIndex) >= 0; ++levelIndex) {
                    currentNode.branches = new List<Node>;
                    Node * tempNode = new Node;
                    tempNode->letter = newKey.at(levelIndex - 1);
                    currentNode.branches.add(*tempNode);
                    currentNode = *tempNode;
                }
                //Store
                currentNode.item = newItem;
                ++size;
                return NULL; //The former item

            } else {
                return currentItem; //The former item;
            }
        }
    } else { //Wrong node
        if (tempList.getLength() > listIndex) {
            return puttanesca(newKey, newItem, tempList, ++listIndex, levelIndex);

        } else {//End of the line, chump
            return NULL; //Tell parent you failed
        }
    }
}

【问题讨论】:

  • 什么是puttanesca(),您为什么使用自定义List 而不是std::list?顺便说一句,不需要有指向列表的指针并动态分配它。使用普通的列表对象会更容易使用。
  • 哦,是的...我正在使用自定义列表,因为我们需要这样做...这是作业的要求,不允许使用标准库中的那些抽象数据类型。嗯...你的意思是我应该让 List * 分支只是 List 分支吧?
  • 关于将List&lt;Node&gt; * 转换为List&lt;Node&gt;,是的。你不需要任何动态。不过,您仍然需要向我们展示puttanesca() 的定义。
  • 好吧,我使用指针是因为我想在 puttanesca 中检查指针是否为 NULL,但我想不需要指针,这只是我缺乏 C++ 经验(?),我我打算把puttanesca 起来。不过它很长。
  • 它检查我的 Trie 的大小...所以,一开始,什么都没有,所以它会返回 true。当我放入更多项目/值时,它会增加。

标签: c++ pointers struct


【解决方案1】:

你的问题在这里:currentNode-&gt;branches-&gt;add(*tempNode);

您插入的是tempNode 的副本,而不是tempNode 本身。您可能要考虑使用List&lt;Node *&gt; branches 而不是List&lt;Node&gt; branches;

【讨论】:

  • 哦哇...我明白了,我完全错过了。谢谢你无限的智慧,这真的让我发疯了。作为一个......“附带问题”,NULL真的只是#define NULL 0 对吗?因此,如果我想检查一个节点是否不存在,除非我使用指针,否则我不能使用 NULL 可以吗?如果我不能使用 NULL,它通常是如何完成的(或者通常是用指针完成的?)再次感谢!
  • 通常用指针来完成。而NULL 是#define;空指针的通用符号是 0(或 C++0x 中的 nullptr)
猜你喜欢
  • 2020-07-27
  • 1970-01-01
  • 2010-12-26
  • 2017-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
相关资源
最近更新 更多