hash就是以空间换时间。如果有冲突就以链表插入 来解决:
#include <iostream>
#include <vector>
using namespace std;
class MyHash
{
public:
class hash_node
{
public:
hash_node():value(0)
,next(nullptr)
{
}
int value;
hash_node* next;
};
MyHash() : m_TableSize(127)
{
m_pHashTable = new hash_node* [m_TableSize];
for (int i = 0; i < m_TableSize; ++i)
{
m_pHashTable[i] = nullptr;
}
}
~MyHash()
{
delete []m_pHashTable;
}
void Insert(int aValue)
{
const int index = GetHashValue(aValue);
hash_node* lpNode;
bool lRes = Search(index,lpNode);
if (!lRes)
{
if (lpNode == nullptr)
{
hash_node* lpNewNode = new hash_node;
lpNewNode->value = aValue;
lpNode->next = lpNewNode;
}
else
{
hash_node* lpNewNode = new hash_node;
lpNewNode->value = aValue;
lpNewNode->next = nullptr;
m_pHashTable[index] = lpNewNode;
}
}
}
bool Search(int aValue, hash_node*& apNode)
{
const int index = GetHashValue(aValue);
auto lpNode = m_pHashTable[index];
if (nullptr == lpNode)
{
return false;
}
if (lpNode->value == aValue)
{
return true;
}
hash_node* lpPreNode = lpNode;
hash_node* lpNextNode = lpPreNode->next;
while (lpNextNode)
{
if (lpNextNode->value == aValue)
{
return true;
}
lpPreNode = lpNextNode;
lpNode = lpPreNode->next;
}
apNode = lpPreNode;
return true;
}
void Find(int value)
{
hash_node* lpNode;
if (Search(value, lpNode))
{
std::cout << "Find out " << value << std::endl;
}
else
{
std::cout << "Not Find.\n";
}
}
private:
int GetHashValue(int aValue)
{
return aValue%m_TableSize;
}
int m_TableSize;
hash_node** m_pHashTable;
};
int main()
{
MyHash lHash;
int lArray[] = {1,45,24,67,33,23,172,151};
int lSize = sizeof(lArray)/sizeof(int);
for (int i = 0; i < lSize; ++i)
{
lHash.Insert(lArray[i]);
}
for (int i = 0; i < lSize; ++i)
{
lHash.Find(lArray[i]);
}
return 0;
}
只是最简单的形式,把key的value混用了,现在把它们分开搞:
#include <iostream>
#include <vector>
using namespace std;
class MyHash
{
public:
class hash_node
{
public:
hash_node():key(0)
, value(0)
,next(nullptr)
{
}
int key;
int value;
hash_node* next;
};
MyHash() : m_TableSize(127)
{
m_pHashTable = new hash_node* [m_TableSize];
for (int i = 0; i < m_TableSize; ++i)
{
m_pHashTable[i] = nullptr;
}
}
~MyHash()
{
delete []m_pHashTable;
}
void Insert(int aKey, int aValue)
{
const int index = GetHashValue(aKey);
hash_node* lpNode;
bool lRes = Search(index,aValue,lpNode);
if (!lRes)
{
if (lpNode == nullptr)
{
hash_node* lpNewNode = new hash_node;
lpNewNode->key = aKey;
lpNewNode->value = aValue;
lpNode->next = lpNewNode;
}
else
{
hash_node* lpNewNode = new hash_node;
lpNewNode->key = aKey;
lpNewNode->value = aValue;
lpNewNode->next = nullptr;
m_pHashTable[index] = lpNewNode;
}
}
}
bool Search(int aKey,int aValue, hash_node*& apNode)
{
const int index = GetHashValue(aKey);
auto lpNode = m_pHashTable[index];
if (nullptr == lpNode)
{
return false;
}
if (lpNode->value == aValue)
{
return true;
}
hash_node* lpPreNode = lpNode;
hash_node* lpNextNode = lpPreNode->next;
while (lpNextNode)
{
if (lpNextNode->value == aValue)
{
return true;
}
lpPreNode = lpNextNode;
lpNode = lpPreNode->next;
}
apNode = lpPreNode;
return true;
}
void Find(int aKey, int aValue)
{
hash_node* lpNode;
if (Search(aKey, aValue, lpNode))
{
std::cout << "Find out " << aValue << std::endl;
}
else
{
std::cout << "Not Find.\n";
}
}
private:
int GetHashValue(int aValue)
{
return aValue%m_TableSize;
}
int m_TableSize;
hash_node** m_pHashTable;
};
int main()
{
MyHash lHash;
int lArray[] = {1,45,24,67,33,23,172,151};
int lSize = sizeof(lArray)/sizeof(int);
for (int i = 0; i < lSize; ++i)
{
lHash.Insert(lArray[i],lArray[i]);
}
for (int i = 0; i < lSize; ++i)
{
lHash.Find(lArray[i],lArray[i]);
}
return 0;
}