Hash template

const  int mod = 9973 ; // usually a prime number close to the total 
struct Hashtable
{
    int x; // value stored in hash 
    Hashtable * next;
    Hashtable()
    {
        next = 0;
    }
};
Hashtable * Hash[mod];
 void Hash_Insert( int x) // store x 
{
     int key = x % mod; // hash function, depending on the situation 
    if (!Hash[key]) // the first element of the 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;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324886035&siteId=291194637