(1)冲突处理方法为:顺次循环后移到下一个位置,寻找空位插入。
(2)BKDE 字符串哈希
unsigned int hash_BKDE(char *str)
{
/* 初始种子seed 可取31 131 1313 13131 131313 etc.. */
unsigned int seed = 131;
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}

选做内容
每一种西文图书都有一个国际标准图书编号,它是一个10位的十进制数字,若要以它作关键字建立一个哈希表,当馆藏书种类不到10,000时,采用折叠法构造一个四位数的哈希函数。

 #include<iostream>
#include<string>
#include<string.h>
#include<stdlib.h> #define Datatype string
#define max 5000
using namespace std; typedef struct
{
Datatype data;
bool isnull;
}Hash;
Hash hashTable[max]; void initHash()
{ for(int i=;i<max;i++)
{
hashTable[i].isnull=;
}
cout<<"call initHash( )\n"<<endl; }
unsigned int hash_B(char *str)
{
/**初始种子seed**/
unsigned int seed=;
unsigned int hash=; while(*str)
{
hash=hash*seed+(*str++);
} return (hash&&0x7fffffff);
}
int insertHash(string str)
{
char ch[];
int index;
strcpy(ch,str.c_str());
index=hash_B(ch); if(hashTable[index].isnull == ) //没有发生冲突
{
hashTable[index].data = ch;
hashTable[index].isnull = ; }
else //当发生冲突的时候
{
while(hashTable[index].isnull == && index<max)
{
index++; //采用线性探测法,步长为1
}
if(index == max) //Hash表发生溢出
return -;
hashTable[index].data = ch;
hashTable[index].isnull = ; }
// cout<<"index: "<<index<<endl;
return ;
//hashTable[index].data=ch;
//hashTable[index].isnull=1;
} bool findHash(string str)
{ char ch[];
int index,i;
strcpy(ch,str.c_str());
index=hash_B(ch);
bool flag=; for(i=index;i<max;i++)
{
if(hashTable[i].isnull==)
{flag=;break;}
if(str==hashTable[i].data)
{flag=;break;}
} return flag; }
int main()
{
initHash();
int n,m,i,j;
cout<<"输入:"<<endl;
cin>>n;
string str;
for(i=;i<n;i++)
{
cin>>str;
if(insertHash(str)<)
{cout<<"溢出"<<endl;break;}
}
cin>>m;
bool e[m];
for(i=,j=;i<m;i++,j++)
{
cin>>str;
if(findHash(str))
e[j]=;
else
e[j]=;
}
for(j=;j<m;j++)
{
if(e[j])
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
cin>>n;
return ;
}
 #include<iostream>
#include<string>
#include<string.h>
#include<stdlib.h> #define Datatype string
#define max 10000
#define length 10
#define adr 4
using namespace std; typedef struct
{
Datatype data;
bool isnull;
}Hash;
Hash hashTable[max]; void initHash()
{ for(int i=;i<max;i++)
{
hashTable[i].isnull=;
}
cout<<"call initHash( )\n"<<endl; }
unsigned int hash_B(char *str)
{
int i,j;
int bit[length];
for(i=;i<length;i++)
{
bit[i]=str[i]-'';
// cout<<bit[i]<<" ";
}
cout<<endl; int ret=;
int temp=;
for(i=;i<adr;i++)
temp=temp*+bit[i];
ret+=temp;
temp=;
for(i=adr;i<adr*;i++)
temp=temp*+bit[i];
ret+=temp;
temp=;
for(i=adr*;i<length;i++)
temp=temp*+bit[i];
ret+=temp;
temp=;
ret=ret%;
cout<<"ret :"<<ret<<endl;
return ret;
}
int insertHash(string str)
{
char ch[];
int index;
strcpy(ch,str.c_str());
index=hash_B(ch); if(hashTable[index].isnull == ) //没有发生冲突
{
hashTable[index].data = ch;
hashTable[index].isnull = ; }
else //当发生冲突的时候
{
while(hashTable[index].isnull == && index<max)
{
index++; //采用线性探测法,步长为1
}
if(index == max) //Hash表发生溢出
return -;
hashTable[index].data = ch;
hashTable[index].isnull = ; }
// cout<<"index: "<<index<<endl;
return ;
//hashTable[index].data=ch;
//hashTable[index].isnull=1;
} bool findHash(string str)
{ char ch[];
int index,i;
strcpy(ch,str.c_str());
index=hash_B(ch);
bool flag=; for(i=index;i<max;i++)
{
if(hashTable[i].isnull==)
{flag=;break;}
if(str==hashTable[i].data)
{flag=;break;}
} return flag; }
int main()
{
initHash();
int n,m,i,j;
cout<<"输入:"<<endl;
cin>>n;
string str;
for(i=;i<n;i++)
{
cin>>str;
if(insertHash(str)<)
{cout<<"溢出"<<endl;break;}
}
cin>>m;
bool e[m];
for(i=,j=;i<m;i++,j++)
{
cin>>str;
if(findHash(str))
e[j]=;
else
e[j]=;
}
for(j=;j<m;j++)
{
if(e[j])
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
cin>>n;
return ;
}

hash表的建立和查找的更多相关文章

  1. 海量路由表能够使用HASH表存储吗-HASH查找和TRIE树查找

    千万别! 非常多人这样说,也包括我. Linux内核早就把HASH路由表去掉了.如今就仅仅剩下TRIE了,只是我还是希望就这两种数据结构展开一些形而上的讨论. 1.hash和trie/radix ha ...

  2. python 字典有序无序及查找效率,hash表

    刚学python的时候认为字典是无序,通过多次插入,如di = {}, 多次di['testkey']='testvalue' 这样测试来证明无序的.后来接触到了字典查找效率这个东西,查了一下,原来字 ...

  3. Hash表的平均查找长度ASL计算方法

    Hash表的“查找成功的ASL”和“查找不成功的ASL” ASL指的是 平均查找时间 关键字序列:(7.8.30.11.18.9.14) 散列函数: H(Key) = (key x 3) MOD 7 ...

  4. 深入了解STL中set与hash_set,hash表基础

    一,set和hash_set简介 在STL中,set是以红黑树(RB-Tree)作为底层数据结构的,hash_set是以哈希表(Hash table)作为底层数据结构的.set可以在时间复杂度为O(l ...

  5. 十一、从头到尾彻底解析Hash 表算法

    在研究MonetDB时深入的学习了hash算法,看了作者的文章很有感触,所以转发,希望能够使更多人受益! 十一.从头到尾彻底解析Hash 表算法 作者:July.wuliming.pkuoliver  ...

  6. 【数据结构】非常有用的hash表

        这篇博客的目的是让尚未学会hash表的朋友们对hash表有一个直观的理解,并且能根据本文定义出属于自己的第一个hash表,但算不上研究文,没有深究概念和成功案例.         什么是has ...

  7. php 数据结构 hash表

    hash表 定义 hash表定义了一种将字符组成的字符串转换为固定长度(一般是更短长度)的数值或索引值的方法,称为散列法,也叫哈希法.由于通过更短的哈希值比用原始值进行数据库搜索更快,这种方法一般用来 ...

  8. Hash 表详解(哈希表)

    散列表(Hash table,也叫哈希表),是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.这个映射函数叫做散列 ...

  9. (面试)Hash表算法十道海量数据处理面试题

    Hash表算法处理海量数据处理面试题 主要针对遇到的海量数据处理问题进行分析,参考互联网上的面试题及相关处理方法,归纳为三种问题 (1)数据量大,内存小情况处理方式(分而治之+Hash映射) (2)判 ...

随机推荐

  1. Delphi 6 Web Services初步评估之二(转)

    Delphi 6 Web Services初步评估之二(转)   ★ 测试环境:CPU:PIII 550内存: 256MBOS: Windows2000 Server + SP2Web Server: ...

  2. SSH隧道技术----端口转发,socket代理

    原文的原始出处不详,本文也是在复制引用了某篇转载,并做了必要的整理与编辑. 本文的受众 如果你遇到了以下问题,那么你应该阅读这篇文章 我听说过这种技术,我对它很感兴趣 我想在家里访问我在公司的机器(写 ...

  3. STL之algorithm、numeric、functional

    <algorithm>是所有STL头文件中最大的一个,其中常用到的功能范围涉及到比较.交换.查找.遍历操作.复制.修改.反转.排序.合并等等. <numeric>体积很小,只包 ...

  4. Google文档

    本博文的主要内容有 .Google文档的介绍 1.Google文档的介绍 https://zh.wikipedia.org/wiki/Google%E6%96%87%E4%BB%B6 http://d ...

  5. Unity3D开发类似保龄球游戏

    先学习一些基本的脚本实现: 1.动态创建物体.默认位置是(0,0)位置 GameObject goNew = GameObject.CreatePrimitive(PrimitiveType.Cube ...

  6. Graphs and Minimum Cuts(Karger's Min-Cut Algorithm)

    Graphs  Two ingredients 1. vertices (nodes) v 2. edges(undirected or directed) Examples: road networ ...

  7. UVA 10985 - Rings'n'Ropes(floyd)

    Problem D Rings'n'Ropes Time Limit: 3 seconds "Well, that seems to be the situation. But, I don ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请 系列目录 创建新表单之后,我们就可以起草申请了,申请按照严格的表单步骤和分 ...

  9. Seeking USB Serial Com Port in Windows Automatically : via PID VID

    After you read previous article, you might know how to operate a com port in Windows.    But that ex ...

  10. [置顶] 提高生产力:开源Java工具包Jodd(Java的”瑞士军刀”)

    官方网站:http://jodd.org/ 下载地址:http://jodd.org/download/index.html Jodd=tools + ioc + mvc + db + aop + t ...