Hash_map的实现

#include<iostream>
using namespace std;

struct Hash {
    size_t operator()(const int &key) {
         return key%3; 
    }
};

struct Compare {
    bool operator() (const int &a, const int &b) const {
        return a == b;
    }
};

template<class Key,class Value>
class Hash_Node {
    public:
        int key;
        int value;
        struct Hash_Node *next;
        Hash_Node(Key key1,Value value1):key(key1),value(value1),next(NULL){}
};

template<class Key, class Value, class Hash, class Compare>
class Hash_Map{
    public:
        Hash_Map(int len = 6):size(len) {
            cur = 0;
            bucket = size/2;
            hash_map = new Hash_Node<Key,Value> *[size];
            for(int i=0;i<bucket;i++)
                hash_map[i] = NULL;
        }
        
        Hash_Node<Key,Value>* find(Key key) {
           int hash_code = hash(key);
           Hash_Node<Key,Value> *pNode = hash_map[hash_code];
           while(pNode) {
                if(compare(pNode->key,key)) {
                       return pNode; 
                } else {
                    pNode = pNode->next;
                }
           }
           return NULL;
        }
         
        bool set(Key key,Value value) {
           if(cur > size)
                return false;
            Hash_Node<Key,Value> *pNode = find(key);
            if(pNode == NULL) {
                int hash_code = hash(key);
                Hash_Node<Key,Value> *pData = new Hash_Node<Key,Value>(key,value);
                Hash_Node<Key,Value> *pnode = hash_map[hash_code];
                if(pnode == NULL) {
                    hash_map[hash_code] = pData;
                } else {
                    while(pnode->next!=NULL) {
                        pnode = pnode->next;
                    }
                    pnode->next = pData;
                }
            } else {
                pNode->value = value;
            }
            ++cur;
            return true;
        }

        Value &get(Key key) {
            Hash_Node<Key,Value> *pnode = find(key);
            if(pnode)
                return pnode->value;
            set(key,0);
            return find(key)->value;
        }

        ~Hash_Map() {
            for(int i=0; i<bucket; i++) {
                while(true) {
                    Hash_Node<Key, Value> *pnode = hash_map[i];
                    if(pnode) {
                       hash_map[i] = pnode->next;
                       delete pnode;
                    } else {
                        break;
                    }    
                }
            }
            delete []hash_map;
        }
    private:
        Hash_Node<Key, Value> **hash_map;
        Hash hash;
        Compare compare;
        int bucket;
        int size;
        int cur;
};

int main() {
    Hash_Map<int, int, Hash, Compare> map;
    map.set(3, 4);
    map.set(5, 6);
    cout << map.get(3) << endl;
    cout << map.get(4) << endl;

    return 0;
}

全部评论

相关推荐

10-02 19:29
已编辑
浙江科技大学 运营
点赞 评论 收藏
分享
09-22 19:21
南京大学 Java
牛客96763241...:刚刚想说才投十几个,养生呢,结果一看是南大本硕✌️,肯定没有问题的
投递小米集团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务