69,373
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
template<class E,class K>
class HashTable
{
public:
HashTable(int divisor=10);
HashTable():D(0),ht(0),empty(0)
{}
~HashTable()
{
delete [] ht;
delete [] empty;
}
bool Search(const K & k,E & e);
bool insert(const E & e);
bool delhash(const K & k);
bool output();
int getsize()
{
return D;
}
private:
int hsearch(const K & k) const;
int D;
E * ht;
bool * empty;
};
template<class E,class K>
int HashTable<E,K>::hsearch(const K & k) const
{
//查找一个地址表中,如果存在,则返回它的位置
//否则,则插入;
int i=k%D;
int j=i;
do
{
if(empty[i]||ht[j]==k)
return j;
j=(j+1)%D;
}
while(j!=1);
return j;
}
//构造函数;
template<class E,class K>
HashTable<E,K>::HashTable(int divisor)
{
D=divisor;
ht=new E[D];
empty=new bool[D];
for(int i=0;i<D;i++)
empty[i]=true;
}