求数据结构大神指教 关于哈希查找

swechy 2017-12-18 05:55:20
题目要求如下

现有十万个由5到9个含大小写英文字符组成姓名的数据库在就业中心,要你设计尽可能优的哈希查找方法,使查找的效率高(即平均查找长度尽可能小),要求最终的平均查找长度不超过5。要求:

1、根据大小写英文字符组成的姓名,构成合适的关键字值(key);

2、设计一个合理的哈希函数;

3、选用一个合适的解决冲突的办法;

4、对于开放定址法,对于建议填充因子在0.6-0.8之间。

5、建立哈希表(关键字和成功查找次数);

6、利用学过的知识直接计算查找成功时的平均查找长度。

7、根据随机生成的测试数据,求出实际情况下的平均查找长度(可能同时包含查找成功或失败的情况)。

这是我写的已经构建好哈希表的算法

但是为什么第68行的 ht.elem[p]=e; 这句代码没办法把 e 的数据传递到 ht 中

#include<iostream>
#include<string>
using namespace std;
const int Maxsize=160000;
const int MaxS=100000;
const int MaxSize=159979;
typedef int KeyType;
const char inputfile[MaxS]="D:\\input.txt";
const char testfile[MaxS]="D:\\test.txt";
const char outputfile[MaxS]="D:\\output.txt";

struct ElemType{
int Key;
string name;
int cnt;
};

struct HashTable
{
ElemType *elem;
};

unsigned int Hash(string s) {
unsigned int h = 0;
for(int i=0; i<s.size(); i++) {
h = 31 * h + s[i];
}
return h % MaxSize;
}

int NextPos(int p)
{
return (p+1)%MaxSize;
}

double sum=0;

bool SearchHash(HashTable ht,string k,int &p)
{
int cnt=1;
p=Hash(k);
while(ht.elem[p].name!="0"&&ht.elem[p].name!=k)
{
cnt++;
p=NextPos(p);
}

if(ht.elem[p].name==k)
{
ht.elem[p].cnt=cnt;
sum=sum+ht.elem[p].cnt;
return true;
}
else
{
return false;
}
}

bool InsertHash(HashTable &ht,ElemType e)
{
int p;
if(SearchHash(ht,e.name,p)==true)
return false;
else
{
ht.elem[p]=e;
return true;
}
}
void Init(HashTable &ht,int n)
{
ht.elem=new ElemType[n];
for(int i=0;i<n;i++)
{
ht.elem[i].Key=0;
ht.elem[i].name="0";
ht.elem[i].cnt=0;
}
}
void create(HashTable &ht,int n)
{
Init(ht,Maxsize);

for(int i=0;i<n;i++)
{
ElemType e;
char s[10];
scanf("%s", s);
e.name=s;
e.Key=Hash(e.name);
InsertHash(ht,e);
}
}
void run()
{
HashTable ht;
int n;
n=100000;
create(ht,n);
}

int main(){

double yinzi,ASL;
cout<<"正在构造哈希表,请稍等......\n\n";
freopen("inputfile","r",stdin);
run();
sum=sum/100000;
ASL=sum;
cout<<sum<<endl;
cout<<"填充因子:";
yinzi=(double)MaxS/(double)Maxsize;
cout<<yinzi<<endl;
cout<<endl;
cout<<"哈希表构造完毕!\n\n";
cout<<"查找成功时的平均查找长度:";
cout<<ASL<<endl;
cout<<endl;
while(true){
freopen("con","r",stdin);
int cmd;
cout<<"=============================================\n";
cout<<"请选择测试方式:\n";
cout<<"=============================================\n";
cout<<"1:文件测试\n";
cout<<"2:键盘输入(输入字符0表示结束测试)\n";
cout<<"0:Return\n";
cin>>cmd;
if(cmd==1){
freopen("testfile","r",stdin);
freopen(outputfile,"w",stdout);
}
else if(cmd==2){
freopen("con","w",stdout);
freopen("con","r",stdin);
}
else if(cmd==0){
break;
}
else{
cout<<"Error input, Try again!\n";
continue;
}
}
return 0;
}

...全文
429 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_43424698 2018-12-11
  • 打赏
  • 举报
回复
大佬,这个源代码还在吗?
oyljerry 2017-12-25
  • 打赏
  • 举报
回复
引用 2 楼 swechy 的回复:
[quote=引用 1 楼 oyljerry的回复:]ht.elem[p]=e; 只赋值了一个地址,你需要对结构体内容进行复制
那请问如何才能把e的内容传到ht里啊 我直接写成ht.elem[i].name=e.name也不行[/quote] 用strcpy来复制name
swechy 2017-12-20
  • 打赏
  • 举报
回复
引用 1 楼 oyljerry的回复:
ht.elem[p]=e; 只赋值了一个地址,你需要对结构体内容进行复制
那请问如何才能把e的内容传到ht里啊 我直接写成ht.elem[i].name=e.name也不行
oyljerry 2017-12-19
  • 打赏
  • 举报
回复
ht.elem[p]=e; 只赋值了一个地址,你需要对结构体内容进行复制

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧