哈希查找算法的源代码c语.pdf
《哈希查找算法的源代码c语.pdf》由会员分享,可在线阅读,更多相关《哈希查找算法的源代码c语.pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
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;str
2、uct 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)cout文件打开失败!n;exit(1);while(!infile.eof()&infile.get()!=EOF)/文件不为空并且文件指针没有
3、指到结束符infile.seekg(Number*sizeof(aNumber),ios:beg);/定位文件指针infile.read(char*)&aNumber,sizeof(aNumber);Number+;infile.close();void Add(precord a)/添加记录 int i,num;cout 当前文件内已有 Number条记录 n;coutnum;ofstream ofile(telphone.txt,ios:app);if(!ofile)cout文件打开失败!;exit(1);for(i=0;inum;i+)cout请输入第 Number+1个人的姓名 aNu
4、mber.name;cout 请输入第 Number+1 个人的电话 aNumber.tel;cout 请输入第 Number+1 个人的地址 aNumber.add;ofile.seekp(ios:end);ofile.write(char*)&aNumber,sizeof(aNumber);Number+;ofile.close();void Print(precord a)/显示所有记录 int i;for(i=0;iNumber;i+)cout 第i+1 个人的信息为:n;cout 姓名:ai.nameendl;cout 电话:ai.telendl;cout 地址:ai.addendl
5、;int Hash(char str)/除留取余 long val=0;char p20,*p1;strcpy(p,str);p1=p;while(*p1!=0)val=val+*p1+;/将字符串中的所有字符对应的ASCII 值相加文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4
6、L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3
7、Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6
8、W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ
9、9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B
10、7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编
11、码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H
12、10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1return(val%Maxsize);int derter;/线性增量int Line_Sollution(int address)/采用线性探测解决冲突 derter+;if(derter=Maxsize)return(-1);else return(address+derter)%Maxsize);int n;int Square_Sollution(int address)/采用平方探测法解决冲突 int j;derter+;if(derter=Maxsize)return-1;n=n*(-1);j=(int(pow(d
13、erter,2)*n+address)%Maxsize;return(j);void Init_Hash(pHashTable h)/初始化哈希表 int i;for(i=0;ielemi=-1;int menu;void Creathash_Name(pHashTable h,precord a)/以用户名为关键字创建哈希表 cout-n;cout 1-以线性探测建表 n;cout 2-以平方探测建表 n;coutmenu;Init_Hash(h);for(i=0;ielemaddress!=-1)if(menu=1)address=Line_Sollution(address);文档编码:
14、CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10
15、N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1
16、 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6
17、Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W1
18、0 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W
19、3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J
20、1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1文档编码:CZ1H10N3H4L1 HN3Q6Y8I6W10 ZQ9W3F9B7J1else address=Square_Sollution(address);if(address=-1)break;if(address!=-1)h-elemaddress=i
21、;h-count+;cout 姓名哈希表已成功建立!n;void Search_Name(pHashTable h,precord a)/查找并显示指定姓名的记录 coutnam;address=Hash(nam);derter=0;n=-1;while(h-elemaddress!=-1&strcmp(nam,ah-elemaddress.name)!=0)if(menu=1)address=Line_Sollution(address);else address=Square_Sollution(address);i+;if(address=-1)break;if(h-elemaddres
22、s!=-1&strcmp(nam,ah-elemaddress.name)=0)cout你要查找的信息为:n;cout 姓名:elemaddress.nameendl;cout 电话:elemaddress.telendl;cout 地址:elemaddress.addendl;cout 比较次数为 iendl;else cout无此姓名,查找失败!;void Creathash_tel(pHashTable h,precord a)/以电话号为关键字创建哈希表 cout-n;cout 1-以线性探测建表 n;cout 2-以平方探测建表 n;coutmenu;Init_Hash(h);for
23、(i=0;ielemaddress!=-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+;cout 电话号哈希表已成功建立!n;void Search_tel(pHashTable h,precord a)/查找并显示指定电话号的记录 couttelphone;address=Hash(telphone);derter=0;n=-1;/初始化线性增量while(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 查找 算法 源代码
限制150内