HELP!我正在尝试使用Separate Chaining创建哈希表。由于某些未知的原因,我似乎无法遍历并找到我加载的所有原始int。我怀疑modulo函数在两个函数中有时会给我错误的地址。首先在创建哈希表时在assorted int上创建坏地址,然后有时在尝试遍历并使用模数再次确认我的列表时搜索第二个函数中的错误地址。哈希表由一个基本的随机数字数组填充,然后我将创建的哈希表与int的原始随机数组进行比较。以下是我认为罪魁祸首造成我所有麻烦但我无法100%确定:
address = randARRAY[key] % MAX_KEYS;
这是使用Separate Chaining创建哈希表的功能。我一般有MAX_KEYS = 5000,tbSIZE = 8989,这比75%的负载系数好55%左右:
void separateCHAINING(int *randARRAY,int tbSIZE,TABLE *head[]){
int key = 0,
address = 0,
collisions = 0,
newONE = 0;
randARRAY[MAX_KEYS + 1] = 0;
TABLE *newADDRESS[tbSIZE];
newADDRESS[tbSIZE] = new TABLE();
for(int a = 0; a < tbSIZE; a++){
newADDRESS[a] = NULL;
head[a] = NULL;
}
while(randARRAY[key] != 0){
address = randARRAY[key] % MAX_KEYS;
newADDRESS[address] = new TABLE;
newADDRESS[address]->key = randARRAY[key];
if(head[address] != 0){
newADDRESS[address]->next = head[address]->next;
head[address]->next = newADDRESS[address];
collisions++;
}
else{
newADDRESS[address]->next = head[address];
head[address] = newADDRESS[address];
newONE++;
}
key++;
}
cout << "total collisions: " << collisions << endl;
cout << "new: " << newONE << endl;
cout << "added: " << collisions + newONE << endl;
cout << "key: " << key << endl;
}
此创建的数据似乎没有问题。我使用gdb在一个数组索引上创建一个非常长的列表,它在第二个函数中都没有丢失任何节点。这就是为什么我认为地址可能会在上面的函数和下面的函数中被modulo搞砸。这显然是在创建伪造的地址,然后再调用错误的地址。最后,我永远无法找到放在哈希表中的随机数组的所有int。下面是再次使用modulo然后尝试遍历并将随机数组与新哈希表进行匹配的函数:
void tableTWO_MATCH(int *randARRAY,TABLE *HT_TWO[]){
int key = 0,
address = 0,
match = 0,
nomatch = 0;
randARRAY[MAX_KEYS + 1] = 0;
while(randARRAY[key] != 0){
address = randARRAY[key] % MAX_KEYS;
while(HT_TWO[address]->next != NULL && HT_TWO[address]->key != randARRAY[key]){
HT_TWO[address] = HT_TWO[address]->next;
}//end second while
if(HT_TWO[address]->key == randARRAY[key]){
match++;
}//end if
if(HT_TWO[address]->key != randARRAY[key]){
nomatch++;
}//end if
key = key + 1;
address = 0;
}//end outer while
cout << "match: " << match << endl;
cout << "not match: " << nomatch << endl;
cout << "key: " << key << endl;
}
一如既往地感谢您提供任何帮助!如果你能看到我弄乱的地方,我将不胜感激!
答案 0 :(得分:1)
嗯,我想我只是个骨头!我使用布尔变量检查并查看在遍历期间的任何时间点是否找到了匹配项。
if(HT_TWO[address]->key == randARRAY[key]){
found = true;
}
我试图匹配已超越其匹配并获得糟糕结果的节点。无论如何这是我改变我的验证使用boolen的方式。谢谢你的帮助!
void tableTWO_MATCH(int *randARRAY,TABLE *HT_TWO[]){
int key = 0,
address = 0,
match = 0,
nomatch = 0;
bool found = false;
randARRAY[MAX_KEYS + 1] = 0;
while(randARRAY[key] != 0){
address = HASH(randARRAY[key],MAX_KEYS);
if(HT_TWO[address]->key == randARRAY[key]){
match++;
}
else{
while(HT_TWO[address]->next != NULL){
HT_TWO[address] = HT_TWO[address]->next;
if(HT_TWO[address]->key == randARRAY[key]){
found = true;
}
}//end second while
if(found == false){
nomatch++;
}
}
key = key + 2;
}//end outer while
cout << "not match: " << nomatch << endl;
cout << "key: " << key << endl;
}