如何從基於數組的哈希表中移除?我需要準備從我的桌子上刪除幾個符號。如果我在一個固定大小的字符數組中刪除了我想要刪除的內容,那麼將如何找到我將「可能」刪除的那些?從HashTable中移除C++
bool hashmap::get(char const * const symbol, stock& s) const
{
int hashVal = this->hashStr(symbol);
int initialHash = -1;
while (hashTable[hashVal].m_symbol != NULL)
{ // try to find a match for the stock associated with the symbol.
if (initialHash == -1)
{
initialHash = hashVal;
}
if (strcmp(hashTable[hashVal].m_symbol, symbol) == 0)
{
s = &hashTable[hashVal];
return true;
}
++hashVal %= maxSize;
}
if (hashTable[hashVal].m_symbol == NULL || hashVal == initialHash)
{
return false;
}
else return true;
}
bool hashmap::put(const stock& s, int& usedIndex, int& hashIndex, int& symbolHash)
{
hashIndex = this->hashStr(s.m_symbol); // Get remainder, Insert at that index.
symbolHash = (int&)s.m_symbol;
usedIndex = hashIndex;
while (hashTable[hashIndex].m_symbol != NULL)
{
++usedIndex %= maxSize; // if necessary wrap index around
if (hashTable[usedIndex].m_symbol == NULL)
{
hashTable[usedIndex] = s;
return true;
}
else if (strcmp(hashTable[usedIndex].m_symbol , s.m_symbol) == 0)
{
return false; // prevent duplicate entry
}
}
hashTable[hashIndex] = s; // insert if no collision
return true;
}
bool hashmap::remove(char const * const symbol)
{
int hashVal = this->hashStr(symbol);
int initialHash = -1;
while (hashTable[hashVal].m_symbol != NULL )
{
if (initialHash == -1)
{
initialHash = hashVal;
}
if (strcmp(hashTable[hashVal].m_symbol, symbol) == 0)
{
hashTable[hashVal].m_symbol = NULL;
return true;
}
++hashVal %= maxSize; // wrap around if needed
} // go to the next cell if not found
if (hashVal != initialHash && hashTable[hashVal].m_symbol != NULL)
{
return true;
}
return false;
}
int hashmap::hashStr(char const * const str)
{
size_t length = strlen(str);
int hash = 0;
for (unsigned i = 0; i < length; i++)
{
hash = 31 * hash + str[i];
}
return hash % maxSize;
}
撤銷了奇數編輯;請至少留下問題可用... – 2009-10-18 20:18:28
我其實想刪除它。 – user40120 2009-10-23 01:48:12
有一些答覆等,你不能刪除一個問題,你爲什麼要刪除它? – GManNickG 2009-10-29 21:28:42