第11章:散列表

下面代码用分离链接法实现散列表:

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值