C++模拟实现HashMap

#ifndef _HASHMAP_H
#define _HASHMAP_H

template<class Key, class Value>
class HashNode
{
public:
    Key _key;
    Value _value;
    HashNode *next;

    HashNode(Key key, Value value)
    {
        _key = key;
        _value = value;
        next = NULL;
    }

    ~HashNode(){}

    HashNode& operator=(const HashNode& node)
    {
        _key = node._key;
        _value = node._value;
        next = node.next;
        return *this;
    }
};

template <class Key, class Value, class HashFunction, class Equals>
class HashMap
{
private:
    HashFunction hash;
    Equals equal;
    HashNode<Key, Value> **table;
    unsigned int _size;
    Value ValueNULL;

public:
    HashMap(int size);
    ~HashMap();
    bool put(const Key& key, const Value& value);
    bool del(const Key& key);
    Value& find(const Key& key);
    void show();
};

template<class Key, class Value, class HashFunction, class Equals>
HashMap<Key, Value, HashFunction, Equals>::HashMap(int size): _size(size), hash(), equal()
{
    table = new HashNode<Key, Value>*[_size];
    for (unsigned i = 0; i < _size; ++ i)
        table[i] = NULL;
}

template<class Key, class Value, class HashFunction, class Equals>
HashMap<Key, Value, HashFunction, Equals>::~HashMap()
{
    HashNode<Key, Value> *currentNode;
    HashNode<Key, Value> *temp;
    for (unsigned i = 0; i < _size; ++ i)
    {
        currentNode = table[i];
        while (currentNode)
        {
            temp = currentNode;
            currentNode = currentNode -> next;
            delete temp;
        }
    }
    delete table;
}

template<class Key, class Value, class HashFunction, class Equals>
bool HashMap<Key, Value, HashFunction, Equals>::put(const Key& key, const Value& value)
{
    int index = hash(key) % _size;
    HashNode<Key, Value> *node = new HashNode<Key, Value>(key, value);
    node -> next = table[index];
    table[index] = node;
    return true;
}

template<class Key, class Value, class HashFunction, class Equals>
bool HashMap<Key, Value, HashFunction, Equals>::del(const Key& key)
{
    unsigned index = hash(key) % _size;
    HashNode<Key, Value> *node = table[index];
    HashNode<Key, Value> *prev = NULL;
    while (node)
    {
        if (node -> _key == key)
        {
            if (prev == NULL) // 说明第一个节点就是我们要删除的节点
            {
                table[index] = node -> next;
            }
            else
            {
                prev -> next = node -> next;
            }
            delete node;
            return true;
        }
        prev = node;
        node = node -> next;
    }
    return false;
}

template<class Key, class Value, class HashFunction, class Equals>
Value& HashMap<Key, Value, HashFunction, Equals>::find(const Key& key)
{
    unsigned index = hash(key) % _size;
    if (table[index] == NULL)
    {
        return ValueNULL;
    }
    HashNode<Key, Value> *node = table[index];
    while(node)
    {
        if (node -> _key == key)
        {
          return node -> _value;
        }
        node = node -> next;
    }
}

template<class Key, class Value, class HashFunction, class Equals>
void HashMap<Key, Value, HashFunction, Equals>::show()
{
    HashNode<Key, Value> *currentNode;
    for (unsigned i = 0; i < _size; ++ i)
    {
        currentNode = table[i];
        while(currentNode)
        {
            std::cout << currentNode -> _key.c_str() << " : " << currentNode -> _value() << std::endl;
            currentNode = currentNode -> next;
        }
    }
}
#endif
 

#include <iostream>
#include "HashMap.h"
#include <string>

using namespace std;

class HashFunction
{
public:
    int operator()(const string& key)
    {
        int hash = 0;
        for (unsigned i = 0, len = key.length(); i< len; ++ i)
        {
            hash = hash << 7 ^ key[i];
        }
        return (hash & 0x7FFFFFFF);
    }
};

class Equals
{
public:
    bool operator()(const string& str1, const string& str2)
    {
        if (str1.compare(str2))
        {
            return true;
        }
        return false;
    }
};

class StuInfo
{
    private:
        string sno;
        string sname;
        string tel;
    public:
        StuInfo(){}
        ~StuInfo(){}
        StuInfo(string sno, string sname, string tel):sno(sno), sname(sname), tel(tel){}
        string operator()()
        {
            string str = "";
            str.append(sno);
            str.append("\t");
            str.append(sname);
            str.append("\t");
            str.append(tel);
            return str;
        }
};

int main()
{
    HashMap<string, StuInfo, HashFunction, Equals> hashMap(16);
    hashMap.put("1001", StuInfo("1001", "张三", "15096098011"));
    hashMap.put("1002", StuInfo("1002", "里斯", "15096098012"));
    hashMap.put("1003", StuInfo("1003", "王五", "15096098013"));
    hashMap.put("1004", StuInfo("1004", "赵柳", "15096098014"));
    hashMap.show();
    cout << "================================" << endl;

    StuInfo sf = hashMap.find("1003");
    cout << sf() << endl;

    return 0;
}
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值