c++,哈希表,创建与查找

哈希表详解,c++

#include
#include<string.h>
using namespace::std;

#define Max_capacity 50
#define SUCCESS 1
#define UNSCESS 0
#define random_six “-1234”

class Hashtable{
public:
Hashtable();
~Hashtable();
int hash(char *t1);
void inithashtable(string *&t);
void inserthashtable(string *&t3,char *t4, int *addr1);
int serchhashtable(string *&t2, char *key1, int *addr2);
private:
string *ct;
char *key;
void *value;
};

Hashtable::Hashtable(){
ct = NULL;
key = NULL;
value = NULL;
}
Hashtable::~Hashtable(){
ct = NULL;
key = NULL;
value = NULL;
}
int Hashtable::hash(char *t1){
char ch1;
ch1 = *t1;
int j = static_cast(ch1);
return j % Max_capacity;
}
void Hashtable::inithashtable(string *&t){
t = new string[Max_capacity];
int j;
for (j = 0; j < 50; j++){
t[j] = random_six;
}
}
void Hashtable::inserthashtable(string *&t3, char *t4,int *addr1){
*addr1 = hash(t4);
while (t3[*addr1] !=random_six){
*addr1 = *addr1 + 1;
}
t3[*addr1] = t4;
}
int Hashtable::serchhashtable(string *&t2, char *key1, int *addr2){
*addr2 = hash(key1);
while (t2[*addr2] != key1){
*addr2 = *addr2 + 1;
if (t2[*addr2] == random_six){
return UNSCESS;
}
else if (*addr2 == hash(key1)){
return UNSCESS;
}
}
return SUCCESS;
}
int main(){
int j;
Hashtable h1;
string *w;
int *temp = new int[50];
h1.inithashtable(w);
char *t[10] = { “beijing”, “baibai”, “shanghai”, “tianjing”, “yunnan”, “hebei”, “shengzheng”, “suzhou”, “guangzhou”, “american” };
for (j = 0; j < 10; j++){
h1.hash(t[j]);
h1.inserthashtable(w, t[j], temp);
}
cout << “------------------------------------------------” << endl;
int u = h1.serchhashtable(w,t[3], temp);
cout << u << endl;
return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值