似乎无法打破碰撞、散列时的 while 循环
我不知道为什么这段代码没有跳出 while 循环:
int table_size = 953;
store hash_table[953];
for(int i = 0; i < table_size; i++)
hash_table[i].count = 0;
//bunch of stuff to get hash value here
while(hash_table[hashNum].data != pString || hash_table[hashNum].count != 0){
hashNum++;
if(hashNum > table_size)
hashNum = 0;
cout << hash_table[hashNum].count;
// to check the value of the count in the array, it IS 0, thus should have broken the loop
}
I don't know why this code is not breaking out of the while loop:
int table_size = 953;
store hash_table[953];
for(int i = 0; i < table_size; i++)
hash_table[i].count = 0;
//bunch of stuff to get hash value here
while(hash_table[hashNum].data != pString || hash_table[hashNum].count != 0){
hashNum++;
if(hashNum > table_size)
hashNum = 0;
cout << hash_table[hashNum].count;
// to check the value of the count in the array, it IS 0, thus should have broken the loop
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能的意思是:
在您的代码中,如果任一情况为 true,则循环将继续,
hash_table[hashNum].count == 0
不足以使子句为 false。you probably mean:
In your code the loop will continue if either case is true,
hash_table[hashNum].count == 0
is NOT sufficient to make the clause false.hash_table[hashNum].count
等于 0 不足以终止循环,因为您在之间使用||
(“or”)终止测试中的两个条件。如果hash_table[hashNum].data
不等于pString
,那么无论hash_table[hashNum].count
是什么,循环都会继续。hash_table[hashNum].count
being equal to zero is not sufficient to terminate the loop since you are using||
("or") between the two conditions in the termination test. Ifhash_table[hashNum].data
is not equal topString
then the loop will continue regardless of whathash_table[hashNum].count
is.我认为你的循环条件应该是
hashNum != 0
而不是hash_table[hashNum].count != 0
。其次,在 while 条件中应该有
&&
而不是||
。这些都是疯狂的猜测,因为这个问题缺少很多信息。
I think your loop condition should be on
hashNum != 0
instead ofhash_table[hashNum].count != 0
.Secondly, there should be
&&
instead of||
in your while condition.These are wild guesses since a lot of information is missing in this question.
您应该看看二元逻辑,尤其是德摩根定理
You should have a look at binary logic, especially De Morgan theorem