C小加

厚德 博学 求真 至善 The bright moon and breeze
posts - 145, comments - 195, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

哈希模板

Posted on 2012-09-12 20:06 C小加 阅读(2147) 评论(0)  编辑 收藏 引用 所属分类: 模板

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

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理