数据结构 hashtable C++实现 链式

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
bool isPrime(int x)
{
    for(int i = 2; i * i <= x; ++i)
    {
        if(x % i == 0)
            return false;
    }
    return true;
}
int nextprime(int x)
{
    for(;;++x)
    {
        if(isPrime(x))
            return x;
    }
}
using namespace std;
template<class HashedObj>
class HashTable
{
public:
    explicit HashTable(int size = 101);
    bool contains(const HashedObj& x) const;
    void makeEmpty();
    bool insert(const HashedObj& x);
    bool remove(const HashedObj& x);
    void printtab() const;
private:
    vector<list<HashedObj> > theLists;
    int currentSize;
    void rehash();
    int myhash(const HashedObj& x) const;
    int hash(const HashedObj& x) const;
};
template<class HashedObj>
HashTable<HashedObj>::HashTable(int size)
{
    theLists.resize(size);
}
template<class HashedObj>
bool HashTable<HashedObj>::contains(const HashedObj& x) const
{
    const list<HashedObj>& whichList = theLists[myhash(x)];
    return find(whichList.begin(), whichList.end(), x) == whichList.end();
}
template<class HashedObj>
void HashTable<HashedObj>::makeEmpty()
{
    for(size_t i = 0; i < theLists.size(); ++i)
        theLists[i].clear();
}
template<class HashedObj>
bool HashTable<HashedObj>::insert(const HashedObj& x)
{
    list<HashedObj> &whichList = theLists[myhash(x)];
    if(find(whichList.begin(), whichList.end(), x) != whichList.end())
        return false;
    whichList.push_back(x);
    if(++currentSize > theLists.size())
rehash();
    return true;
}
template<class HashedObj>
bool HashTable<HashedObj>::remove(const HashedObj& x)
{
    list<HashedObj> &whichList = theLists[myhash(x)];
    typename::list<HashedObj>::iterator itr = find(whichList.begin(), whichList.end(), x);
    if(itr == whichList.end())
        return false;
    whichList.erase(itr);
    --currentSize;
    return true;
}
template<class HashedObj>
void HashTable<HashedObj>::rehash()
{
    vector<list<HashedObj> > oldLists = theLists;
    theLists.resize(nextprime(2 * theLists.size()));
    for(size_t j = 0; j < theLists.size(); ++j)
        theLists[j].clear();
    currentSize = 0;
    for(size_t i = 0; i < oldLists.size(); ++i)
    {
        typename list<HashedObj>::iterator itr = oldLists[i].begin();
        while(itr != oldLists[i].end())
            insert(*itr++);
    }
}
template<class HashedObj>
int HashTable<HashedObj>::myhash(const HashedObj& x) const
{
    int hashval = hash(x);
    hashval %= theLists.size();
    if(hashval < 0)
        hashval += theLists.size();
    return hashval;
}
template<class HashedObj>
void HashTable<HashedObj>::printtab() const
{
    for(size_t i = 0; i < theLists.size(); ++i)
    {
        copy(theLists[i].begin(), theLists[i].end(), ostream_iterator<HashedObj>(cout, " "));
        if(!theLists[i].empty())
        cout << endl;
    }
}
template<>
int HashTable<int>::hash(const int& x) const
{
    return x % theLists.size();
}

template<>
int HashTable<string>::hash(const string& x) const
{
    return x[0] % theLists.size();
}
int main()
{
    return 0;
}

转载于:https://my.oschina.net/u/196018/blog/383771

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值