您的位置:首页 > 其它

哈希表实现

2016-05-01 12:20 281 查看
哈希表实现:

使用BKDRHash作为基础的哈希函数,同时使用拉链法作为冲突处理方法,实现哈希表的插入和查找操作。

哈希函数BKDRHash实现如下:

//映射方法,BKDRHash方法
static int32_t BKDRHash(const string& str){
int32_t hash=0,seed = 131;
for(uint32_t i=0;i<str.length();i++){
hash = hash * seed + str[i];
}
return (hash & 0x7fffffff);
}


处理冲突的方法有拉链法,开放定址和再哈希法。各种冲突方法如何实现,可以自行查资料进行学习,这里不做详细介绍。另附一张图,说明拉链法(说明版权:来源小象学院huge的算法讲解PPT)。



程序实现:

/***************************************
FileName :HashTable.cpp
Author : godfrey
CreatedTime : 2016/5/1
****************************************/
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class hash_node{
public:
hash_node() {
this->data = "";
this->next = NULL;
}
~hash_node() {}
void SetData(string data){
this->data = data;
}
string GetData(){
return this->data;
}
void SetNext(hash_node* next){
this->next = next;
}
hash_node* GetNext(){
return this->next;
}
private:
string data;
hash_node* next;
};

class hash_table{
public:
hash_table(int32_t hash_table_len,int32_t node_pool_len){
this->hash_table_len = hash_table_len;
this->node_pool_len = node_pool_len;
this->node_pool_cnt = 0;
try{
this->hash_table_main = new hash_node*[this->hash_table_len];
memset(this->hash_table_main,0,sizeof(hash_node*)*(this->hash_table_len));
this->hash_node_pool = new hash_node[this->node_pool_len];
}catch(const bad_alloc & e){
std::cerr<<"there is no more place"<<endl;
}
}
~hash_table(){
delete[] this->hash_table_main;
delete[] this->hash_node_pool;
}
//哈希表的插入操作,使用拉链法
bool insert(string str,bool& Is_uniqe){
if(true == this->search(str)){
Is_uniqe = false;
return true;
}
//拉链法插入
hash_node* p;
hash_node* q = this->GetNewNode();
if(q == NULL) return false;
int32_t index = this->GetHashIndex(str);
p = this->hash_table_main[index];
q->SetData(str);
q->SetNext(p);
this->hash_table_main[index] = q;
Is_uniqe = true;
return true;
}
//哈希表的查找操作
bool search(string str){
int32_t index = this->GetHashIndex(str);
hash_node* p = this->hash_table_main[index];
while(p){
if(p->GetData()==str) return true;
p = p->GetNext();
}
return false;
}
private:
hash_node** hash_table_main;//哈希表的大小
hash_node* hash_node_pool;//哈希表的内存池

int32_t hash_table_len,node_pool_len,node_pool_cnt;
//映射方法,BKDRHash方法
static int32_t BKDRHash(const string& str){
int32_t hash=0,seed = 131;
for(uint32_t i=0;i<str.length();i++){
hash = hash * seed + str[i];
}
return (hash & 0x7fffffff);
}
//获取哈希表中字符串相应的索引
int32_t GetHashIndex(string& str){
if(hash_table_len == 0) return 0;
return (hash_table::BKDRHash(str)%this->hash_table_len);
}
//在内存池中获取新的节点内存
hash_node* GetNewNode(){
if(this->node_pool_cnt>=this->node_pool_len) return NULL;
return &this->hash_node_pool[node_pool_cnt++];
}
};

int main()
{
hash_table ht(100,1000);
bool is_uniqe,flag;
ht.insert("godfrey",is_uniqe);
cout<<"ht.insert godfrey : "<<is_uniqe<<endl;
ht.insert("good",is_uniqe);
cout<<"ht.insert good : "<<is_uniqe<<endl;
ht.insert("godfrey",is_uniqe);
cout<<"ht.insert godfrey twice : "<<is_uniqe<<endl;
flag = ht.search("godfrey");
cout<<"ht.search godfrey : "<<flag<<endl;
return 0;
}


运行结果:



转载请注明出处:

C++博客园:godfrey_88

http://www.cnblogs.com/gaobaoru-articles/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: