Hash模板

const int mod = 9973;//一般为靠近总数的素数
struct Hashtable
{
    int x;//hash存的值
    Hashtable * next;
    Hashtable()
    {
        next = 0;
    }
};
Hashtable * Hash[mod];
void Hash_Insert(int x)//存x
{
    int key = x % mod;//hash函数,根据情况而定
    if(!Hash[key])//该key第一个元素
    {
        Hashtable * p = new Hashtable;
        p->x = x;
        Hash[key] = p;
    }
    else
    {
        Hashtable *p = Hash[key];
        while(p->next)p=p->next;
        Hashtable* temp = new Hashtable;
        temp->x = x;
        p->next = temp;
    }
}
bool Find(int x)
{
    int key = x % mod;
    if(!Hash[key])return false;
    else
    {
        Hashtable * temp = Hash[key];
        while(temp)
        {
            if(temp->x == x)
                return true;
            temp = temp->next;
        }
    }
    return false;
}

猜你喜欢

转载自www.cnblogs.com/fzl194/p/8949505.html