下面代码用分离链接法实现散列表:
1:将一个输入元素利用散列函数散列到某一个值:
int hash(int key)
{
return key;
}
int hash(const string& key)
{
int hashVal=0;
for(string::size_type ix=0;ix!=key.size();ix++)
hashVal=37*hashVal+key[ix];
return hashVal;
}
template<class Type>
int HashTable<Type>::myHash(const Type& key) const
{
int hashVal=hash(key);
//vector<list<Type> > theLists
hashVal%=theLists.size(); // 用到的是除法散列法,所以这个哈希表的size()为素数
if(hashVal<0)
hashVal+=theLists.size();
return hashVal;
}
2:插入操作:
代码如下:
template<class Type>
bool HashTable<Type>::insert(const Type& val)
{
list<Type>& whichList=theLists[myHash(val)];
if(find(whichList.begin(),whichList.end(),val)!=whichList.end())
return false;
whichList.push_back(val);
if(++currentSize>theLists.size())
rehash(); // 装载因子超过了1,将其复制到另外一个
size()更大的哈希表中,rehash()函数下面会给出
return true;
}
3:查找某一个元素是否已经包含在该哈希表中:
template<class Type>
bool HashTable<Type>::contains(const Type& val) const
{
const list<Type>& whichList=theLists[myHash(val)];
// if(find(whichList.begin(),whichList.end(),val)!=whichList.end())
// return true;
// return false;
return find(whichList.begin(),whichList.end(),val)!=whichList.end();
}
4:删除操作
template<class Type>
bool HashTable<Type>::remove(const Type& val)
{
list<Type>& whichList=theLists[myHash(val)];
typename list<Type>::iterator it=find(whichList.begin(),whichList.end(),val);
if(it==whichList.end())
return false;
whichList.erase(it);
--currentSize;
return true;
}
5:如果该哈希表中装载因子达到某一个值后,就不能再插入元素了,如果还要再插入元素,就将其散列到另外一个哈希表中
template<class Type>
void HashTable<Type>::rehash()
{
vector<list<Type> > oldLists=theLists;
theLists.resize(nextPrime(2*theLists.size()));
for(typename vector<list<Type> >::size_type ix=0;ix!=theLists.size();ix++)
theLists[ix].clear();
currentSize=0;
for(typename vector<list<Type> >::size_type ix=0;ix!=oldLists.size();ix++)
for(typename list<Type>::const_iterator it=oldLists[ix].begin();it!=oldLists[ix].end();it++)
insert(*it);
}