1、哈希查找算法的源代码 c 语言【问题描述】针对自己的班集体中的“人名”设计一个哈希表,使得平均查找长度不超过R,完成相应的建表和查表程序。基本要求假设人名为中国姓名的汉语拼音形式。待填入哈希表的人名共有 30 个,取平均查找长度的上限为 2。哈希函数用除留余数法构照,用链表法处理冲突。测试数据读取熟悉的 30 个人的姓名。#include #include #include using namespace std;#define Maxsize 57 struct record char name20;char tel20;char add20;typedef record * precord
2、;struct HashTable int elemMaxsize; /存放数组 a的下标int count; ;typedef HashTable * pHashTable;int Number; /统计当前数组 a中的记录总数void Getdata(precord a) /从文件 telphone.txt 中读取数据存放到数组 a Number=0;ifstream infile(“telphone.txt“,ios:in|ios:binary);if(!infile) coutnum;ofstream ofile(“telphone.txt“,ios:app);if(! ofile)
3、coutaNumber.name; coutaNumber.tel; coutaNumber.add; ofile.seekp(ios:end);ofile.write(char *)Number+;ofile.close();void Print(precord a) /显示所有记录 int i;for(i=0;ielemi=-1;int menu;void Creathash_Name(pHashTable h,precord a)/以用户名为关键字创建哈希表 coutmenu;Init_Hash(h);for(i=0;ielemaddress!=-1)if(menu=1) address
4、=Line_Sollution(address);else address=Square_Sollution(address);if(address=-1) break;if(address!=-1) h-elemaddress=i; h-count+;coutnam;address=Hash(nam);derter=0;n=-1;while(h-elemaddress!=-1 else address=Square_Sollution(address);i+;if(address=-1) break;if(h-elemaddress!=-1 Init_Hash(h);for(i=0;iele
5、maddress!=-1)if(menu=1) address=Line_Sollution(address);else address=Square_Sollution(address);if(address=-1) break;if(address!=-1) h-elemaddress=i; h-count+;couttelphone;address=Hash(telphone);derter=0; n=-1; /初始化线性增量while(h-elemaddress!=-1 else address=Square_Sollution(address);i+;if(address=-1) b
6、reak;if(h-elemaddress!=-1 switch(menu1) case 0:system(“cls“);exit();break;case 1:Add(a);system(“pause“);system(“cls“);goto start;break;case 2:Print(a);system(“pause“);system(“cls“);goto start;break;case 3:Creathash_Name(H,a);system(“pause“);system(“cls“);goto start;break;case 4:Creathash_tel(H,a);system(“pause“);system(“cls“);goto start;break;case 5:Search_Name(H,a);system(“pause“);system(“cls“);goto start;break;case 6:Search_tel(H,a);system(“pause“);system(“cls“);goto start;break;default:cout“请输入正确的操作选项!n“;system(“cls“);goto start;break; return 0;