Day_10,key值为字符串的字典(Direction)实现

之前写过一篇简单的字典的实现,哈希表的原理及利用hash实现简单的字典
今天写的是key值为字符串的字典,并对之前简单字典实现做一些优化。

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

class HashNode{
public:
    string  mKey;
    string  mValue;
    HashNode *next;

    HashNode(string key, string value){
        mKey   = key;
        mValue = value;
        next = NULL;
    }
    ~HashNode(){
    }
    HashNode& operator=(const HashNode& node){
        if(this == &node) return *this;
        mKey = node.mKey;
        mValue = node.mValue;
        next = node.next;
        return *this;
    }
};

class HashMap{
public:
    HashMap(int size);
    ~HashMap();
    bool HMInsert(const string& key, const string& value);
    bool HMDelete(const string& key);
    string& HMFind(const string& key);
    string& operator[](const string& key);
private:
    int hashfunc(const string& key);
    HashNode ** mTable;
    int mSize;
    string strnull;
};

//数组初始化
HashMap::HashMap(int size):mSize(size){
    //:mSize(size)作用等效于mSize = size;
    mTable = new HashNode*[size];
    for(int i=0; i<mSize; ++i){
        mTable[i] = NULL;
    }
    strnull = "NULL";
}
//析构函数
HashMap::~HashMap(){
    for(int i=0; i<mSize; ++i){
        HashNode *curNode = mTable[i];
        //删除当前节点及链接法产生的所有节点
        while(curNode){
            HashNode *temp = curNode;
            curNode =curNode->next;
            delete temp;
        }
    }
    delete mTable;
}
//此处声明为引用形参
//const string & 表明 首先这个形参你不会修改;其次这个形参是一个引用,减少副本的复制
bool HashMap::HMInsert(const string& key, const string& value)
{
    int index = hashfunc(key)%mSize;
    HashNode *node = new HashNode(key, value);
    //这是一个互相引用?   mTable[index]指向 node;node->next指向mTable[index]
    //下面作用举例
    //假设此时节点A进来
    //当mTable[index]为空的时候,等价于 mTable[index]->A->Null
    //假设此时mTable[index]->A,节点B进来,等价于 mTable[index]->B->A->Null
    //所以这里其实是使用了上篇所讲的 ”链接法“
    node->next = mTable[index];
    mTable[index] = node;
    return true;
}

bool HashMap::HMDelete(const string &key)
{
    int index = hashfunc(key)%mSize;
    HashNode *node = mTable[index];
    HashNode *prev = NULL; //用来记录上一个节点
    //大概就是这样一个过程
    //A->B->C 有这样一个链表
    //若此时A就是我们要找的地址,则让mTable[index]->next 指向B  并且删除A
    //若此时B是我们要找的地址,则让A->next指向C 并且删除B
    while(node){
        if(key == node->mKey){
            if(NULL == prev){
                mTable[index] = node->next;
            }else{
                prev->next = node->next;
            }
            delete node;
            return true;
        }
        prev = node;
        node = node->next;
    }
    return false;
}

string& HashMap::HMFind(const string& key)
{
    int index = hashfunc(key)%mSize;
    if(NULL == mTable[index]){
        return strnull;
    }else{
        HashNode *node = mTable[index];
        //遍历链表,找到key值相同的返回value
        while(node){
            if(key == node->mKey){
                return node->mValue;
            }
            node = node->next;
        }
    }
    return strnull;
}

string& HashMap::operator[](const string& key)
{
    return HMFind(key);
}

//哈希函数
//可以考虑1、根据初始化的数组长度使用不同的hash函数
//2、动态数组动态改变hash函数(估计没戏,每次都重新分布的话,需要删除旧的,还要重新赋值新的,会有很大的时间浪费) 说不定什么时候能用上呢?
//后面会带来一篇关于hash函数的文章,例如下面的hash函数可以归类为 “位运算Hash”
int HashMap::hashfunc(const string& key){
    int hash = 0;
    for(int i=0; i<key.length(); ++i){
        hash = hash << 7^key[i];
    }
    return (hash & 0x7FFFFFFF);
}


int main()
{
    HashMap hashmap(10);//可以采用C#中的List,给一个默认长度,如果没有声明,然后动态两倍增长长度

    hashmap.HMInsert("Hello", "World");
    hashmap.HMInsert("why", "dream");
    hashmap.HMInsert("c++", "good");
    hashmap.HMInsert("welcome", "haha");

    cout << "after insert:" << endl;
    //c_str():生成一个const char*指针,指向以空字符终止的数组。
    //c_str()函数返回一个指向正规c字符串的指针,内容和string类的本身对象是一样的,通过string类的c_str()函数能够把string对象转换成c中的字符串的样式;
    //这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
    //注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
    //下面的c_str()是可以删去的
    /*
    比如:最好不要这样:
    char* c;
    string s="1234";
    c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理
  应该这样用:
    char c[20];
    string s="1234";
    strcpy(c,s.c_str());
  这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作
    */
    cout << hashmap.HMFind("welcome").c_str() << endl;
    cout << hashmap.HMFind("c++").c_str() << endl;
    cout << hashmap["why"].c_str() << endl;
    cout << hashmap["Hello"].c_str() << endl;

    if (hashmap.HMDelete("Hello"))
        cout << "remove is ok" << endl;
    cout << hashmap.HMFind("Hello").c_str() << endl;
    hashmap["why"] = "love";
    cout << hashmap["why"].c_str() << endl;
    return 0;
}

本文代码引自https://blog.csdn.net/feng973/article/details/79277713
代码的含义在代码中已经表出,接下去的一篇将会讲解hash表的核心 hash函数的一些经验及研究

一个人在外打拼还是辛苦的呀,加油! =3= 今天我姐在家里自己做烧鸡和蛋挞!啊!羡慕呀
写这篇博客的第二天,又是12点下班的一天,继续拖更

广州最近又开始不太平了, 希望everybody be healthy

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值