zoukankan      html  css  js  c++  java
  • 线性探查法实现的散列表(哈希表)类

    const int DefaultSize = 100;
    enum KindofStatus {Active, Empty, Deleted};

    template<class E, class K>
    class HashTable
    {
        public:
            HashTable(int d, int sz = DefaultSize);
            ~HashTable() {delete []ht_; delete []info;}
            HashTable<E, K>& operator = (const HashTable<E, K>& rhs);
            bool Search(const K k1, const E& el) const;
            bool Insert(const E& el);
            bool Remove(const K k1, const E& el);
            void makeEmpty();
        private:
            int divitor_;
            int current_size_;
            int table_size_;
            E* ht_;
            KindofStatus* info;
            int find_pos(const K k1) const;
            int operator == (E& el) {return *this == el;}
            int operator != (E& el) {return *this != el;}
    };

    template<class E, class K>
    int HashTable<E, K>::find_pos(const K k1) const
    {
        int i = k1%divitor_;
        int j = i;
        do
        {
            if(info[j] == Empty || info[j] == Active && ht_[j] == k1)
                return j;
            j = (j + 1) % table_size_;
        }while(j !=i);
        return j;
    }

    template<class E, class K>
    bool HashTable<E, K>::Search(const K k1, const E& el) const
    {
        int i = find_pos(k1);
        if(info[i] != Active || ht_[i] != k1)
            return false;
        el = ht_[i];
        return true;
    }

    template<class E, class K>
    void HashTable<E, K>::makeEmpty()
    {
        for(int i=0; i<table_size_; i++)
        {
            info[i] = Empty;
            current_size_ = 0;
        }
    }

    template<class E, class K>
    bool HashTable<E, K>::Insert(const E& el)
    {
        K k1 = el;  // overload
        int i = find_pos(k1);
        if(info[i] != Active)
        {
            ht_[i] = el;
            current_size_ ++;
            info[i]++;
            return true;
        }
        if(info[i] == Active && ht_[i] == k1)
        {
            cout<<"this key has been contained"<<endl;
            return false;
        }
        cout<<"the table is full and can not be insert!"<<endl;
        return false;
    }

    template<class E, class K>
    HashTable<E, K>& HashTable<E, K>::operator = (const HashTable<E, K>& rhs)
    {
        if( *this != rhs)
        {
            delete []ht_;
            delete []info;
            table_size_ = rhs.table_size_;
            divitor_ = rhs.divitor_;
            current_size_ = rhs.current_size_;
            ht_ = new E[table_size_];
            info = new KindofStatus[table_size_];
            for(int i=0; i<table_size_; i++)
            {
                ht_[i] = rhs.ht_[i];
                info[i] = rhs.info[i];
            }

        }
        return *this;
    }

    template<class E, class K>
    bool HashTable<E, K>::Remove(const K k1, const E& el)
    {
        int i = find_pos(k1);
        if(info[i] == Active && ht_[i] == k1)
        {
            info[i] = Deleted;
            current_size_--;
            return true;
        }
        return false;
    }

  • 相关阅读:
    201671030116宋菲菲 实验三作业互评与改进报告
    通读《构建之法》提出问题
    201671010460-朱艺璇-实验四附加实验
    201671010460朱艺璇 词频统计软件项目报告
    201671010460朱艺璇 实验三作业互评与改进报告
    阅读《现代软件工程—构建之法》提出的问题
    手把手带你了解消息中间件(3)——RocketMQ
    字符编码的历史由来
    linux常用命令
    linux各目录及重要目录的详细介绍
  • 原文地址:https://www.cnblogs.com/kex1n/p/2286585.html
Copyright © 2011-2022 走看看