我正在编写线性和二次探测哈希表程序.
这是一个用于线性探测功能的for-loop,它工作得很好.
//when there's a collision increase i by 1 until finding empty slot
for(i = (hashVal % tableSize+1) % tableSize; i <tableSize; i++)
if(a[i] == -1){
a[i] = hashVal;
break;
}
Run Code Online (Sandbox Code Playgroud)
所以我在二次探测函数中再次写了一个for循环来处理碰撞
//when there's a collision increase i by i^2
j = 0;
for(i=((hashVal % tableSize+1) % tableSize); i < tableSize; i++)
j = i^2;
if(a[j] == -1){
a[j] = hashVal;
break;
}
Run Code Online (Sandbox Code Playgroud)
但是当我编译二次探测时,我收到了这个错误
error: 'break' statement not in loop or switch statement
Run Code Online (Sandbox Code Playgroud)
我真的很困惑,为什么它导致第二个错误,而线性探测是好的.谁有人解释为什么?