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

#include <iostream>
#include <vector>
using namespace std;

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;
    }
}

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);
    enum EntryType{ACTIVE, EMPTY, DELETED};
    void printhash() const;
private:
    struct HashEnty
    {
        HashedObj element;
        EntryType info;
        HashEnty(const HashedObj &e = HashedObj(), EntryType i = EMPTY) :
                element(e), info(i){}
    };
    vector<HashEnty> array;
    int currentSize;
    bool isActive(int currentPos) const;
    int findPos(const HashedObj& x) const;
    void rehash();
    int myhash(const HashedObj& x) const;
    int hash(const int& x) const;
    int hash(const string& x) const;
};
template<class HashedObj>
HashTable<HashedObj>::HashTable(int size) : array(nextprime(size))
{
    makeEmpty();
}
template<class HashedObj>
bool HashTable<HashedObj>::contains(const HashedObj& x) const
{
    return isActive(findPos(x));
}
template<class HashedObj>
void HashTable<HashedObj>::makeEmpty()
{
    currentSize = 0;
 for(size_t i = 0; i < array.size(); ++i)
        array[i].info = EMPTY;
}
template<class HashedObj>
bool HashTable<HashedObj>::insert(const HashedObj& x)
{
   int currentPos = findPos(x);
   if(isActive(currentPos))
        return false;
    array[currentPos] = HashEnty(x, ACTIVE);
    if(++currentSize > array.size() / 2)
        rehash();
    return true;

}
template<class HashedObj>
bool HashTable<HashedObj>::remove(const HashedObj& x)
{
    int currentPos = findPos(x);
    if(!isActive(currentPos))
        return false;
    array[currentPos].info = DELETED;
    return true;
}
template<class HashedObj>
bool HashTable<HashedObj>::isActive(int currentPos) const
{
    return array[currentPos].info == ACTIVE;
}
template<class HashedObj>
int HashTable<HashedObj>::findPos(const HashedObj& x) const
{
    int offset = 1;
    int currentPos = myhash(x);
    while(array[currentPos].info != EMPTY && array[currentPos].element != x)
    {
        currentPos += offset;
        offset += 2;
        if(currentPos >= array.size())
            currentPos -= array.size();
    }
    return currentPos;
}
template<class HashedObj>
void HashTable<HashedObj>::rehash()
{
    vector<HashEnty> oldArray = array;
    array.resize(nextprime(2 * oldArray.size()));
    for(size_t i = 0; i < array.size(); ++i)
        array[i].info = EMPTY;
    currentSize = 0;
    for(size_t i = 0; i < oldArray.size(); ++i)
        if(oldArray[i].info == ACTIVE)
            insert(oldArray[i].element);
}
template<class HashedObj>
int HashTable<HashedObj>::myhash(const HashedObj& x) const
{
    int cnt = 0;
    int hashVal = hash(x);
    while(isActive(hashVal))
        hashVal = (hash(x) + (++cnt)) % array.size();
    return hashVal;
}
template<>
int HashTable<int>::hash(const int& x) const
{
    int hashVal = x % array.size();
    if(hashVal < 0)
        hashVal += array.size();
    return hashVal;
}
template<>
int HashTable<string>::hash(const string& x) const
{
    int hashVal = 0;
    for(size_t i = 0; i < x.length(); ++i)
        hashVal = 37 * hashVal + x[i];
    hashVal %= array.size();
    if(hashVal < 0)
        hashVal += array.size();
    return hashVal;
}
template<class HashedObj>
void HashTable<HashedObj>::printhash() const
{
    for(size_t i = 0; i != array.size(); ++i)
        cout << array[i].element << " ";
}
int main()
{
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值