在传递函数中的节点时通过引用传递问题

Pass by reference issue in passing the nodes in the function

#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <string>

using namespace std;

struct node {
        bool indicator;
        map<int, struct node*> vec; 
};

node * create_node () {
        node *newnode = new node();
        newnode -> indicator = 0;
        for(int i = 1; i <= 26; i++)
            newnode -> vec[i] = NULL;
        return newnode;
}

void insert_node (node* insertNode, int str_size, int currentIndex, string str) {
        node *newNode;
        newNode = create_node();
        int int_convert = str[currentIndex] % 96;
        insertNode -> vec[int_convert] = newNode;
        currentIndex++;
        if(currentIndex == str_size) {
            insertNode -> vec[int_convert] -> indicator = 1;
            cout<<"String added in the dictionary with certain insertions\n";
        }
        else
            insert_node(insertNode -> vec[int_convert], str_size, currentIndex, str);
}

void travel (node* currentNode, int str_size, int currentIndex, string str) {
        if(currentIndex != str_size) {
            int int_convert = str[currentIndex] % 96;
            if (currentNode -> vec[int_convert])
                travel(currentNode -> vec[int_convert], str_size, ++currentIndex, str);
            else {
                char c;
                cout<<"String not present\n"<<"Do u want 2 build (Y/N)\n";
                cin>>c;
                if (c == 'Y')
                    insert_node(currentNode, str_size, currentIndex, str);
            }
        }
        else {
            if(currentNode -> indicator == 1)
                cout<<"String FOund in DIctionary\n";
            else {
                cout<<"String not in dictionary BUT can acheived without further insertions\n";
                currentNode -> indicator = 1;
                cout<<"String stored in dictionary\n";
            }
        }
}

int main() {
        int num;
        char C;
        cout<<"enter number of insertions\n";
        scanf("%d", &num);
        node *rootnode;
        rootnode = create_node();
        for(int i =0; i < num; i++) {
            string S;
            cout<<"Enter string\n";
            scanf("%c", &C);
            getline(cin, S);
            travel(rootnode, S.size(), 0, S);
        }
        return 0;
}

在上面的代码中,当给出超过 2 个输入时,树的根将丢失,新树再次从 NULL 形成,消除由先前值形成的树,因此我认为问题是通过节点到函数,节点地址没有得到保留。所以请弄清楚,这是一个 Trie 程序。例如 字符串输入 1 - "top" 最初树是空的,所以 "top" 不会出现,因此 top 将被插入树中以供将来遍历。 字符串输入 2 - "top" 这次它会打印“在字典中找到因为 "top" 已经在第一次输入中被输入,直到这里程序响应良好 字符串输入 3 - "top" 当第三个输入再次 "top" 时,输出应再次为 "found in dictionary" 但输出“未找到”并且再次插入 "top",因此树对两个输入的响应正确一次,意味着第 4 个输入将对应于第 3 个输入,但它不会考虑第 1 个或第 2 个输入,类似地,在第 5 个输入上,树再次变为空并插入字符串,第 6 个输入将仅对第 5 个输入的存在产生影响。

算法本身没有明显错误。一个问题是您混淆了 iostream 和传统的 C I/O 函数并且做错了。

假设我有这个输入:

3
top
top
top

scanf("%d", &num); 只读取数字 3,没有换行符。第一次进入循环时,scanf("%c", &C); 读取换行符,然后 getline(cin, S); 正确读取字符串 "top"。然而,下一次执行循环时,换行符 已经被 getline 函数读取 ,因为它读取了整行。所以代码 scanf("%c", &C); 改为读取 "top" 的第一个字符:'t'.

仅选择 I/O 库之一的正确方法:cin/coutscanf/printf,并始终如一地在任何地方使用它。要读取字符串,只需使用 cin >> sscanf("%s", s),不要将其与 getline.

混淆