• 线性探查法实现的散列表(哈希表)类


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

  • 相关阅读:
    测试精进之路
    访问Apache服务器自动跳转到https协议
    如何安装Chrome OS系统
    遇到Project Target Framework Not Installed解决方法
    2019开发者调查结果和总结
    「工具神器」推荐一个扒网页的神器
    Linux 使用Unzip提示write error (disk full?). Continue? (y/n/^C)的解决方法
    安卓编译Failed to authenticate Jack server certificate错误问题解决办法
    如何手动给APK文件签名
    如何给个人网站添加免费的SSL
  • 原文地址:https://www.cnblogs.com/kex1n/p/2286585.html
Copyright © 2020-2023  润新知