Bool method returns invalid value

I created a method bool contains(string)for a hash table of a linked list that checks if a value is in a hash. I use a helper function for recursion, but when the helper function returns false, it bool contains(string)still returns true. I passed it through the debugger, and I clearly see that it returns false, and I'm not sure why.

This searches for the current node:

"laccoliths"->"morbiferous"->"oculi"->"unscabbarded"

the meaning i'm looking for is this "typung".

Here is the code:

bool contains_h(string x, node * p) //helper method
{
    if (p == NULL)
        return false;
    else if (x == p->data)
        return true;
    else
        contains_h(x, p->next);
}

bool contains(string word) { return contains_h(word, head); }

+4
source share
1 answer

Nice downtime. You forgot to put "return" in the final statement:

bool contains_h(string x, node * p) //helper method
{
    if (p == NULL)
        return false;
    else if (x == p->data)
        return true;
    else
        return contains_h(x, p->next);
}


, , :
bool contains_h(string x, node * p) //helper method
{
    return ((p!=NULL) && (x == p->data || contains_h(x, p->next)));
}

. , , .

+7

Source: https://habr.com/ru/post/1614005/


All Articles