Rom*_*pez 4 c++ boolean-logic boolean return-value boolean-expression
我bool contains(string)
为链表散列表创建了一个方法,用于检查值是否在散列中.我使用辅助函数来递归,但是当辅助函数返回时false
,bool contains(string)
仍然返回true.我通过调试器运行它,我可以清楚地看到它返回false,我不知道为什么.
这是当前搜索的节点:
"laccoliths"->"morbiferous"->"oculi"->"unscabbarded"
我正在寻找的价值是"typung"
.
这是代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
bool contains(string word) { return contains_h(word, head); }
很简单的一个.你忘了把'return'放在最后的陈述上:
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);
}
Run Code Online (Sandbox Code Playgroud)
bool contains_h(string x, node * p) //helper method
{
return ((p!=NULL) && (x == p->data || contains_h(x, p->next)));
}
Run Code Online (Sandbox Code Playgroud)
就个人而言,我更愿意阅读你的六行.但是,其他人可能不同意,特别是因为它可以避免遗漏的退货声明问题.