散列表 哈希表.docx

上传人:b****4 文档编号:4417906 上传时间:2022-12-01 格式:DOCX 页数:9 大小:16.75KB
下载 相关 举报
散列表 哈希表.docx_第1页
第1页 / 共9页
散列表 哈希表.docx_第2页
第2页 / 共9页
散列表 哈希表.docx_第3页
第3页 / 共9页
散列表 哈希表.docx_第4页
第4页 / 共9页
散列表 哈希表.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

散列表 哈希表.docx

《散列表 哈希表.docx》由会员分享,可在线阅读,更多相关《散列表 哈希表.docx(9页珍藏版)》请在冰豆网上搜索。

散列表 哈希表.docx

散列表哈希表

散列表设计

针对某个集体中人名设计一个散列表,使得平均查找长度不超过R,并完成相应的建表和查表程序。

#include

#include

#include

#defineL50/*定义哈希表长*/

#defineM47/*定义p值*/

#defineN30/*定义名单长*/

charz[22];

structold{char*name;char*py;intk;};

structoldoldlist[L];/*原始表*/

structhterm

{char*name;char*py;

intk;intsi;

};

structhtermhlist[L];/*哈希表*/

inti,adr,sum,d;

charch1;

floataverage;

/**********************************/

voidchash()

{for(i=0;i

{hlist[i].name="";

hlist[i].py="";

hlist[i].k=0;

hlist[i].si=0;

};

for(i=0;i

{sum=0;

adr=(oldlist[i].k)%M;

d=adr;

if(hlist[adr].si==0)

{hlist[adr].k=oldlist[i].k;

hlist[adr].name=oldlist[i].name;

hlist[adr].py=oldlist[i].py;

hlist[adr].si=1;

}

else

{do

{d=(d+((oldlist[i].k))%10+1)%M;/*伪随机*/

sum=sum+1;

}

while(hlist[d].k!

=0);

hlist[d].k=oldlist[i].k;

hlist[d].name=oldlist[i].name;

hlist[d].py=oldlist[i].py;

hlist[d].si=sum+1;

}

}

}

/***************************************/

voidfindhlist()

{ints0;charr,g;

clrscr();/*清屏*/

for(r=0;r<20;r++){z[r]=0;};

gotoxy(1,1);printf("查找:

copyrightby姚建飞2003.6");

gotoxy(5,10);printf("请拼音后回车!

");

gotoxy(5,12);scanf("%s",z);

s0=0;

for(r=0;r<20;r++){s0=z[r]+s0;};

gotoxy(5,13);printf("%d",s0);

/*for(i=0;i

sum=1;

adr=s0%M;

d=adr;

if(hlist[adr].k==s0)

{

gotoxy(18,18);printf("");

gotoxy(18,18);printf("%s",hlist[d].name);

gotoxy(18,19);printf("%s",hlist[d].py);

gotoxy(18,20);

printf("搜索%d次",sum);

getch();

}

else

{if(hlist[adr].k==0)

{gotoxy(18,18);

printf("无记录!

");

getch();

}

else

{g=0;

for(i=0;g==0;i++)

{d=(d+s0%10+1)%M;/*伪随机*/

sum=sum+1;

if(hlist[d].k==0)

{gotoxy(18,18);

printf("无记录!

");

g=1;getch();

};

gotoxy(18,18);

printf("%s",hlist[d].name);

gotoxy(18,19);

printf("%s",hlist[d].py);

gotoxy(18,20);

printf("搜索%d次",sum);

getch();

if(hlist[d].k==s0)

{g=1;

gotoxy(18,21);

printf("搜索%d次成功!

",sum);

getch();

};

};

};

};

}

/***************************************/

voidinp()/*输入表*/

{

char*f;

intr,s0;

oldlist[0].name="桂芳芳";oldlist[0].py="guifanfan";

oldlist[1].name="姚建飞";oldlist[1].py="yaojianfei";

oldlist[2].name="杨扬";oldlist[2].py="yangyang";

oldlist[3].name="朱玉环";oldlist[3].py="zhuyuhuang";

oldlist[5].name="陈曦";oldlist[5].py="chenxi";

oldlist[6].name="张雷";oldlist[6].py="zhanglei";

oldlist[7].name="盛永海";oldlist[7].py="shenyonghai";

oldlist[8].name="陈道全";oldlist[8].py="chengdaoquan";

oldlist[9].name="陆道清";oldlist[9].py="ludaoqing";

oldlist[10].name="龚云祥";oldlist[10].py="gongyunxiang";

oldlist[11].name="孙振兴";oldlist[11].py="sunzhenxing";

oldlist[12].name="孙容飞";oldlist[12].py="sunrongfei";

oldlist[13].name="孙明龙";oldlist[13].py="sunminglong";

oldlist[14].name="张浩";oldlist[14].py="zhanghao";

oldlist[15].name="田苗";oldlist[15].py="tianmiao";

oldlist[16].name="姚建中";oldlist[16].py="yaojianzhong";

oldlist[17].name="姚建清";oldlist[17].py="yaojianqing";

oldlist[18].name="姚建华";oldlist[18].py="yaojianhua";

oldlist[19].name="张海峰";oldlist[19].py="yaohaifeng";

oldlist[20].name="陈言号";oldlist[20].py="chengyanhao";

oldlist[21].name="姚秋锋";oldlist[21].py="yaoqiufeng";

oldlist[22].name="钱鹏程";oldlist[22].py="qianpengcheng";

oldlist[23].name="姚海峰";oldlist[23].py="yaohaifeng";

oldlist[24].name="卞艳";oldlist[24].py="bianyan";

oldlist[25].name="凌蕾";oldlist[25].py="linglei";

oldlist[26].name="李伟";oldlist[26].py="liwei";

oldlist[27].name="黄海燕";oldlist[27].py="huanhaiyan";

oldlist[28].name="刘殿琴";oldlist[28].py="liudianqin";

oldlist[29].name="李云";oldlist[29].py="liyun";

/*

请在此输入数据,同时修改程序开头的M LN

*/

for(i=0;i

{

s0=0;

f=oldlist[i].py;

for(r=0;*(f+r)!

='\0';r++){s0=*(f+r)+s0;};

oldlist[i].k=s0;

};

}

/****************************************/

voiddhash()/*显示哈希表*/

{charLON=17;

clrscr();

if(LON>L){LON=L;};

gotoxy(1,1);printf("哈希表:

copyrightby姚建飞2003.6");

gotoxy(1,2);printf("地址:

");

for(i=0;i

{gotoxy(1,i+3);

printf("%-3d",i);

};

gotoxy(9,2);printf("关键字:

");

for(i=0;i

{gotoxy(10,i+3);

printf("%-6d",hlist[i].k);

};

gotoxy(19,2);printf("姓名:

");

for(i=0;i

{gotoxy(19,3+i);

printf("%s",hlist[i].name);

};

gotoxy(28,2);printf("拼音:

");

for(i=0;i

{gotoxy(28,i+3);

printf("%s",hlist[i].py);

};

gotoxy(40,2);printf("搜索长度:

");

for(i=0;i

{gotoxy(43,i+3);

printf("%2d",hlist[i].si);

};

gotoxy(53,2);printf("H(key):

");

for(i=0;i

{gotoxy(53,i+3);

printf("%2d",(hlist[i].k)%M);

};

average=0;

for(i=0;i

{average=average+hlist[i].si;};

average=average/N;

gotoxy(10,23);

printf("平均搜索长度:

ASL(%d)=%f",N,average);

gotoxy(20,24);

printf("任意键下一屏!

");

ch1=getch();

if(L>15)

{

clrscr();

if(LON>L-15){LON=L-15;};

gotoxy(1,1);printf("哈希表:

copyrightby姚建飞2003.6");

gotoxy(1,2);printf("地址:

");

for(i=0;i

{gotoxy(1,i+3);

printf("%-3d",i+15);

};

gotoxy(9,2);printf("关键字:

");

for(i=0;i

{gotoxy(10,i+3);

printf("%-6d",hlist[i+15].k);

};

gotoxy(19,2);printf("姓名:

");

for(i=0;i

{gotoxy(19,3+i);

printf("%s",hlist[i+15].name);

};

gotoxy(28,2);printf("拼音:

");

for(i=0;i

{gotoxy(28,i+3);

printf("%s",hlist[i+15].py);

};

gotoxy(40,2);printf("搜索长度:

");

for(i=0;i

{gotoxy(43,i+3);

printf("%2d",hlist[i+15].si);

};

gotoxy(53,2);printf("H(key):

");

for(i=0;i

{gotoxy(53,i+3);

printf("%2d",(hlist[i+15].k)%M);

};

average=0;

for(i=0;i

{average=average+hlist[i].si;};

average=average/N;

gotoxy(10,23);

printf("平均搜索长度:

ASL(%d)=%f",N,average);

gotoxy(20,24);

printf("任意键下一屏!

");

ch1=getch();

};

if(L>30)

{

clrscr();

if(LON>L-30){LON=L-30;};

gotoxy(1,1);printf("哈希表:

copyrightby姚建飞2003.6");

gotoxy(1,2);printf("地址:

");

for(i=0;i

{gotoxy(1,i+3);

printf("%-3d",i+30);

};

gotoxy(9,2);printf("关键字:

");

for(i=0;i

{gotoxy(10,i+3);

printf("%-6d",hlist[i+30].k);

};

gotoxy(19,2);printf("姓名:

");

for(i=0;i

{gotoxy(19,3+i);

printf("%s",hlist[i+30].name);

};

gotoxy(28,2);printf("拼音:

");

for(i=0;i

{gotoxy(28,i+3);

printf("%s",hlist[i+30].py);

};

gotoxy(40,2);printf("搜索长度:

");

for(i=0;i

{gotoxy(43,i+3);

printf("%2d",hlist[i+30].si);

};

gotoxy(53,2);printf("H(key):

");

for(i=0;i

{gotoxy(53,i+3);

printf("%2d",(hlist[i+30].k)%M);

};

average=0;

for(i=0;i

{average=average+hlist[i].si;};

average=average/N;

gotoxy(10,23);

printf("平均搜索长度:

ASL(%d)=%f",N,average);

gotoxy(20,24);

printf("任意键下一屏!

");

ch1=getch();

};

if(L>45)

{

clrscr();

if(LON>L-45){LON=L-45;};

gotoxy(1,1);printf("哈希表:

copyrightby姚建飞2003.6");

gotoxy(1,2);printf("地址:

");

for(i=0;i

{gotoxy(1,i+3);

printf("%-3d",i+45);

};

gotoxy(9,2);printf("关键字:

");

for(i=0;i

{gotoxy(10,i+3);

printf("%-6d",hlist[i+45].k);

};

gotoxy(19,2);printf("姓名:

");

for(i=0;i

{gotoxy(19,3+i);

printf("%s",hlist[i+45].name);

};

gotoxy(28,2);printf("拼音:

");

for(i=0;i

{gotoxy(28,i+3);

printf("%s",hlist[i+45].py);

};

gotoxy(40,2);printf("搜索长度:

");

for(i=0;i

{gotoxy(43,i+3);

printf("%2d",hlist[i+45].si);

};

gotoxy(53,2);printf("H(key):

");

for(i=0;i

{gotoxy(53,i+3);

printf("%2d",(hlist[i+45].k)%M);

};

average=0;

for(i=0;i

{average=average+hlist[i].si;};

average=average/N;

gotoxy(10,23);

printf("平均搜索长度:

ASL(%d)=%f",N,average);

gotoxy(20,24);

printf("任意键返回!

");

ch1=getch();

};

}

/**************************************/

voidmain()

{inp();/*输入原表*/

chash();/*建哈希表*/

a:

clrscr();

gotoxy(21,2);

textcolor(GREEN);

cprintf("欢迎使用本程序------------编者:

姚建飞");

printf("\n");

gotoxy(22,4);

textcolor(GREEN);

cprintf("1.显示哈希表");

printf("\n");

gotoxy(22,6);

textcolor(GREEN);

cprintf("2.查找");

printf("\n");

gotoxy(22,8);

textcolor(GREEN);

cprintf("x.退出");

printf("\n");

gotoxy(22,12);

cprintf("请输入选择:

");

printf("\n");

gotoxy(24,14);

ch1=getch();

if(ch1==0x78){textcolor(GREEN);

cprintf("谢谢使用本程序,你已经退出本程序!

");printf("\n");exit();};/*"x":

退出*/

if(ch1==0x31){dhash();};/*表的属性*/

if(ch1==0x32){findhlist();};/*查找*/

gotoa;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1