1. 头文件
enum KindOfEntry
{
Deleted, Empty, Legitimate
};
struct Node
{
int key;
KindOfEntry info;
};
class MyHash
{
public:
MyHash(int tableSize);
~MyHash();
void Insert(int key);
void Delete(int key);
Node *Find(int key);
private:
Node *_hash;
int _tableSize;
int _capacity;
bool _IsFull();
int _FindPos(int key);
int _HashKey(int key);
int _GetPrime(int num);
bool _IsPrime(int num);
void _Rehash();
};
2. 源文件
MyHash::MyHash(int tableSize)
{
_tableSize = _GetPrime(tableSize);
_capacity = 0;
_hash = new Node[_tableSize];
for(int i = 0; i < _tableSize; i++)
{
_hash[i].info = Empty;
_hash[i].key = -9999; //假设没有-9999关键字
}
}
MyHash::~MyHash()
{
delete [] _hash;
}
int MyHash::_HashKey(int key)
{
return key % _tableSize;
}
int MyHash::_FindPos(in