hashTable实现

上文:http://hi.baidu.com/syxcs123/blog/item/4165aa51f6a87873853524bf.html

      看了网上介绍的hashTable,平时用的都是现成的库,实际上自己真的没有很是明白,自己就写了个简单的,虽然没实现O(1),但应该也可以的吧!一下图是原理和简单的封装 和用STL的版本 的代码!


image

#include <iostream>
using namespace std;

class HashTable{
public :
    enum ItemState {NULLKEY , HAVEKEY , DELEDKEY} ;
    struct NextData{
        char * data;
        NextData *next;
    };
    struct Item{
        ItemState is;
        NextData * data;
    };
public :
    HashTable(int size);
    void initHashTsble();
    bool insert(char * e);
    bool serch(char *e);
    bool del(char *e);
    int hash(char * e);
private :
    int hashSize ;
    Item * data;
};
HashTable::HashTable(int size):hashSize(size){
    initHashTsble();
}
void HashTable::initHashTsble(){
    int i ;
    data = new Item[hashSize];
    for(i=0; i<hashSize; i++){
        data[i].is = NULLKEY;
        data[i].data = NULL;
    }
}
int HashTable::hash(char * e){
    const char * it = e;
    unsigned long key = 0;
    while( *it != 0 )
    { 
        key = (key<<5) + key + *it++;
    }
    return ( key % hashSize);
}
bool HashTable::insert(char * e){
    int key = hash(e);
    if(data[key].is != HAVEKEY){
        data[key].is = HAVEKEY;
        NextData *nd = new NextData();
        nd->data = e;
        nd->next = NULL;
        data[key].data = nd;
    }else {
        NextData * nd = data[key].data;
        while(nd->next) {
            nd = nd->next;
        }
        NextData * ndTemp = new NextData();
        ndTemp->data = e;
        ndTemp->next = NULL;
        nd->next = ndTemp;
    }
    return true;
}
bool HashTable::serch(char *e)
{
    int key = hash(e);
    if(data[key].is == HAVEKEY){
        NextData *nd = data[key].data;
        while(nd->next){
            if(strcmp(nd->data , e) == 0)
                return true;
            else
                nd = nd->next;
        }
    }
    return false;
}
bool HashTable::del(char *e){
    int key = hash(e);
    if(data[key].is == HashTable::ItemState::HAVEKEY){
        NextData * nd = data[key].data;
        NextData *ndFront = NULL;
        while(nd){
            if(strcmp(nd->data , e) == 0){
                if(nd->next) {
                    NextData *ndTemp = nd;
                    nd = nd->next;
                    delete ndTemp;    
                }else {
                    ndFront->next = NULL;
                    delete nd;
                }
                return true;
            }
            ndFront = nd;
            nd = nd->next;
        }
    }
    return false;
}
int main(){
    HashTable hs(100);
    char *p1 = "syx";
    char *p2 = "yxs";
    char *p3 = "ysx";
    char *p4 = "my";
    hs.insert(p1); 
    hs.insert(p2);
    hs.insert(p3);
    cout<<hs.serch(p3)<<endl;
    cout<<hs.del(p4)<<endl;
    cout<<hs.del(p3)<<endl;
    cout<<hs.serch(p3)<<endl;
    return 0;
}

STL版本:

C语言: Codee#15837

#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;

class HashTable{
public :
    enum ItemState {NULLKEY , HAVEKEY , DELEDKEY} ;
    struct Item{
        ItemState is;
        list<string> data;
    };
public :
    HashTable(int size);
    void initHashTsble();
    bool insert(string e);
    bool serch(string e);
    bool del(string e);
    int hash(string e);
private :
    int hashSize ;
    vector<Item> data;
};
HashTable::HashTable(int size):hashSize(size){
    initHashTsble();
}
void HashTable::initHashTsble(){
    int i ;
    data = vector<Item>(hashSize);
    for(i=0; i<hashSize; i++){
        data[i].is = HashTable::ItemState::NULLKEY;
        data[i].data = list<string>();
    }
}
int HashTable::hash(string e){
    const char * it = e.c_str();
    unsigned long key = 0;
    while( *it != 0 )
    { 
        key = (key<<5) + key + *it++;
    }
    return ( key % hashSize);
}
bool HashTable::insert(string e){
    int key = hash(e);
    data[key].is = HashTable::ItemState::HAVEKEY;
    data[key].data.push_back(e);
    return true;
}
bool HashTable::serch(string e)
{
    int key = hash(e);
    if(data[key].is == HashTable::ItemState::HAVEKEY){
        list<string>::iterator itBegin = data[key].data.begin();
        list<string>::iterator itEnd = data[key].data.end();
        while(itBegin != itEnd){
            if(*itBegin == e){
                return true;
            }
            ++itBegin;
        }
    }
    return false;
}
bool HashTable::del(string e){
    int key = hash(e);
    if(data[key].is == HashTable::ItemState::HAVEKEY){
        data[key].data.remove(e);
    }
    return true;
}
int main(){
    HashTable hs(100);
    string p1 = "syx";
    string p2 = "yxs";
    string p3 = "ysx";
    string p4 = "my";
    hs.insert(p1); 
    hs.insert(p2);
    hs.insert(p3);
    cout<<hs.hash("faefae")<<endl;
    cout<<hs.hash("gaeg")<<endl;
    cout<<hs.hash("afefa")<<endl;
    cout<<hs.hash("faedgae")<<endl;
    cout<<hs.hash("123")<<endl;
    cout<<hs.serch(p3)<<endl;
    //cout<<hs.del(p4)<<endl;
    cout<<hs.del(p3)<<endl;
    cout<<hs.serch(p3)<<endl;
    return 0;
}


打造最快的Hash

http://blog.chinaunix.net/u3/104957/showart_2079562.html

http://www.360doc.com/content/05/0819/10/332_7235.shtml#


作者:syxChina
出处:http://syxchina.cnblogs.com/

本文章是作者学习心得笔记,欢迎转载,请注明原文地址,如有疑问,可以通过 278250658@qq.com 联系作者本人。

原文地址:https://www.cnblogs.com/syxchina/p/2197268.html