如何解决如何从打开的地址哈希表中返回键比较?
下面是我的哈希表代码。我遇到的问题是在删除键时返回正确数量的键比较,有时也用于插入键。例如,对于一个测试用例,程序应该返回 2 个关键比较,但它只返回 1 个。
int HashInsert(int key,HashSlot hashTable[])
{
int i = 1;
int keyComparisons = 0;
int index = 0;
HashSlot *newNode;
index = hash1(key);
if (hashTable[index].key == key)
return -1;
if (hashTable[index].indicator == EMPTY) {
newNode = (HashSlot *)malloc(sizeof(HashSlot));
newNode->key = key;
newNode->indicator = USED;
hashTable[index] = *newNode;
return 0;
}
else {
if (hashTable[index].indicator == USED) {
keyComparisons++;
index = hash1(key + hash2(key));
}
if (hashTable[index].key == key)
return -1;
hashTable[index].key = key;
hashTable[index].indicator = USED;
return keyComparisons;
}
}
int HashDelete(int key,HashSlot hashTable[])
{
int index = hash1(key);
int keyComparisons = 0;
int i = 0;
while (hashTable[index].key != key) {
index++;
if (index == TABLESIZE)
return -1;
break;
}
while (hashTable[index].key != key) {
keyComparisons+=1;
index = hash1(key + i*hash2(key));
i++;
}
if (hashTable[index].indicator == DELETED)
return -1;
hashTable[index].indicator = DELETED;
return keyComparisons;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。