如何从打开的地址哈希表中返回键比较?

问题描述

下面是我的哈希表代码。我遇到的问题是在删除键时返回正确数量的键比较,有时也用于插入键。例如,对于一个测试用例,程序应该返回 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 (将#修改为@)