我正在学习哈希表并在另一个网站上找到此代码,但我无法理解Insert(int key,int value)函数。 该功能运行良好,但我想知道是否有额外的代码不需要或我是不是完全理解它。
具体来说,函数末尾的else条件:
else
{
entry->value = value;
}
当我使用不同的参数调用该函数时,似乎无法达到该条件。这是代码的其余部分。
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>
using namespace std;
const int TABLE_SIZE = 128;
class HashNode
{
public:
int key;
int value;
HashNode* next;
HashNode(int key, int value)
{
this->key = key;
this->value = value;
this->next = NULL;
}
};
class HashMap
{
private:
HashNode** htable;
public:
HashMap()
{
htable = new HashNode*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
htable[i] = NULL;
}
~HashMap()
{
for (int i = 0; i < TABLE_SIZE; ++i)
{
HashNode* entry = htable[i];
while (entry != NULL)
{
HashNode* prev = entry;
entry = entry->next;
delete prev;
}
}
delete[] htable;
}
/*
* Hash Function
*/
int HashFunc(int key)
{
return key % TABLE_SIZE;
}
/*
* Insert Element at a key
*/
void Insert(int key, int value)
{
int hash_val = HashFunc(key);
HashNode* prev = NULL;
HashNode* entry = htable[hash_val];
while (entry != NULL)
{
prev = entry;
entry = entry->next;
}
if (entry == NULL)
{
entry = new HashNode(key, value);
if (prev == NULL)
{
htable[hash_val] = entry;
}
else
{
prev->next = entry;
}
}
else
{
entry->value = value;
}
}
/* Search Element at a key
*/
int Search(int key)
{
bool flag = false;
int hash_val = HashFunc(key);
HashNode* entry = htable[hash_val];
while (entry != NULL)
{
if (entry->key == key)
{
cout << entry->value << " ";
flag = true;
}
entry = entry->next;
}
if (!flag)
return -1;
}
};
int main()
{
HashMap hash;
hash.Insert(3, 7);
hash.Search(3);
}
任何澄清都非常感谢。
谢谢
答案 0 :(得分:2)
while (entry != NULL)
先于
if (entry == NULL)
除非while (entry != NULL)
为entry
,否则无法退出NULL
循环,从而保证else
案例不可能发生。
我相信在while
循环内部需要进行测试以确定密钥是否已经存在。
while (entry != NULL)
{
if (entry->key == key)
{
break;
}
prev = entry;
entry = entry->next;
}
偏离主题:请查看此问题并回答有关如何简化代码的建议:Using pointers to remove item from singly-linked list
示例:
/*
* Insert Element at a key
*/
void Insert(int key, int value)
{
int hash_val = HashFunc(key);
HashNode** nextPtr = &htable[hash_val]; // Get a pointer to next, not to the entry
// with a pointer to next we can keep going from next to next without
// ever needing a previous.
// pick up a whole bunch of extra dereferencing *nextPtr, but an
// optimizing compiler is great at dealing with that extra overhead.
while ((*nextPtr) != NULL) // loop until found or end
{
if ((*nextPtr)->key == key) // found key already in list
{
(*nextPtr)->value = value; // set value for key
return; // all done.
}
nextPtr = &(*nextPtr)->next; // keep looking
}
*nextPtr = new HashNode(key, value); // didn't find. Add new node to end.
}
附录:Search
仅在失败案例中返回。声明为返回值的函数必须始终返回一个值。