哈希模板(template)

来源:互联网 发布:瞻博网络 上海 电话 编辑:程序博客网 时间:2024/06/05 02:47
const int  MAX=1000003;template <class T>class hash{private:    int pos;    int next[MAX];    int head[MAX];    T key[MAX];public:    hash();    bool search(T x);    void push(T x);};template <class T>hash<T>::hash(){    pos=0;    memset(next,-1,sizeof(next));    memset(head,-1,sizeof(head));    //memset(key,-1,sizeof(key));}template <class T>inline bool hash<T>::search(const T x){    int temp=x%MAX;    int t=head[temp];    while(t!=-1)    {        if (key[t]==x)        {            return 1;        }        t=next[t];    }    return 0;}template <class T>inline void hash<T>::push(const T x){    int temp=x%MAX;    if (head[temp]!=-1)    {        next[pos]=head[temp];    }    head[temp]=pos;    key[pos]=x;    pos++;}

from C小加

0 0