當前位置:成語大全網 - 漢語詞典 - 哈希表設計字典查詢

哈希表設計字典查詢

#包括

& ltstdio.h & gt

# include & ltconio.h & gt

# include & ltctype.h & gt

#define L 50 /*定義哈希表長度*/

#定義M 47 /*定義p值*/

#define N 30 /*定義列表長度*/

char z[22];

結構舊的{ char * namechar * pyint k;};

構造舊old list[L];/*原始表格*/

結構hterm

{ char * namechar * py

int k;int si

};

struct hterm hlist[L];/*哈希表*/

int i,adr,sum,d;

char ch 1;

浮動平均值;

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

void chash()

{ for(I = 0;我& ltl;i++)

{hlist[i]。name =

hlist[i]。py =

hlist[i]。k = 0;

hlist[i]。si = 0;

};

for(I = 0;我& ltn;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]。姓名;

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

hlist[adr]。si = 1;

}

其他

{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]。姓名;

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

hlist[d]。si = sum+1;

}

}

}

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

void findhlist()

{ int s0char r,g;

clr SCR();/*清空屏幕*/

for(r = 0;r & lt20;r++){ z[r]= 0;};

gotoxy(1,1);printf("研究.....");

gotoxy(5,10);printf("輸入

名稱的拼寫:");

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

s0 = 0;

for(r = 0;r & lt20;r++){ s0 = z[r]+s0;};

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

/* for(I = 0;我& ltl;i++)*/

sum = 1;

ADR = s0 % M;

d = adr

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

{

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

gotoxy(18,18);printf(" %s ",hlist[d]。姓名);

gotoxy(18,19);printf("

%s ",hlist[d]。py);

gotoxy(18,20);

printf("查找%d次",sum);

getch();

}

其他

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

{gotoxy (18,18);

printf("無關於

它!");

getch();

}

其他

{ 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]。姓名);

gotoxy(18,19);

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

gotoxy(18,20);

printf("查找

%d倍”,總和);

getch();

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

{ g = 1;

gotoxy(18,21);

printf("查找%d次

直到成功!“,求和);

getch();

};

};

};

};

}

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

Void inp() /*輸入表*/

{

char * f;

int r,s0;

舊列表[0]。name = " A舊列表[0]。py= "蘋果";

舊列表[1]。name = " B舊列表[1]。py = " bus

舊列表[2]。name = " C舊列表[2]。py= "貓";

舊列表[3]。name = " D舊列表[3]。py= "狗";

舊列表[5]。name = " E舊列表[5]。py = " egg

舊列表[6]。name = " F舊列表[6]。py = " fly

舊列表[7]。name = " G舊列表[7]。py= "好";

舊列表[8]。name = " H舊列表[8]。py= "傷害";

舊列表[9]。name = " I舊列表[9]。py = " int

舊列表[10]。name = " J舊列表[10]。py = " joy

舊列表[11]。name = " K舊列表[11]。py = " keep

舊列表[12]。name = " L舊列表[12]。py = " long

舊列表[13]。name = " M舊列表[13]。py = " make

舊列表[14]。name = " N舊列表[14]。py = " net

舊列表[15]。name = " O舊列表[15]。py = " out

舊列表[16]。name = " P舊列表[16]。py= "倒";

舊列表[17]。name = " Q舊列表[17]。py = " queen

舊列表[18]。name = " R舊列表[18]。py= "運行";

舊列表[19]。name = " S舊列表[19]。py= "孫";

舊列表[20]。name = " T舊列表[20]。py= "茶";

舊列表[21]。name = " U舊列表[21]。py= "直到";

舊列表[22]。name = " V舊列表[22]。py = " vection

舊列表[23]。name = " W舊列表[23]。py= "水";

舊列表[24]。name = " X舊列表[24]。py =射線";

舊列表[25]。name = " Y舊列表[25]。py= "妳";

舊列表[26]。name = " Z舊列表[26]。py = " zoo

舊列表[27]。name = " AA舊列表[27]。py = " aah

舊列表[28]。name = " BB舊列表[28]。py = " bbc

舊列表[29]。name = " CC舊列表[29]。py = " cch

/*請在此輸入數據,並修改M L N*/

for(I = 0;我& ltn;i++)

{

s0 = 0;

f=oldlist[i]。py;

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

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

舊列表[i]。k = s0

};

}

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

Void dhash() /*顯示哈希表*/

{夏爾LON = 17;

clr SCR();

如果(LON & gt;L){ LON = L;};

gotoxy(1,1);printf(" HashTable:");

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

for(I = 0;我& ltLON;i++)

{gotoxy(1,I+3);

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

};

gotoxy(9,2);printf(" H(鍵)

是:“);

for(I = 0;我& ltLON;i++)

{gotoxy(10,I+3);

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

};

gotoxy(19,2);printf(" name:");

for(I = 0;我& ltLON;i++)

{gotoxy(19,3+I);

printf("%s ",hlist[i]。姓名);

};

gotoxy(28,2);printf(" spell:");

for(I = 0;我& ltLON;i++)

{gotoxy(28,I+3);

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

};

gotoxy(40,2);printf("the

長度:“);

for(I = 0;我& ltLON;i++)

{gotoxy(43,I+3);

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

};

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

for(I = 0;我& ltLON;i++)

{gotoxy(53,I+3);

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

};

平均值= 0;

for(I = 0;我& ltl;i++)

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

平均值=平均值/N;

gotoxy(10,23);

printf("ASL:ASL(%d)=%f ",N,平均值);

gotoxy(20,24);

printf("任意鍵通過");

ch 1 = getch();

如果(L & gt15)

{

clr SCR();

如果(LON & gt;L-15){ LON = L-15;};

gotoxy(1,1);printf(" HashTable:");

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

for(I = 0;我& ltLON;i++)

{gotoxy(1,I+3);

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

};

gotoxy(9,2);printf(" H(鍵)

是:“);

for(I = 0;我& ltLON;i++)

{gotoxy(10,I+3);

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

};

gotoxy(19,2);printf(" name:");

for(I = 0;我& ltLON;i++)

{gotoxy(19,3+I);

printf("%s ",hlist[i+15]。姓名);

};

gotoxy(28,2);printf(" spell:");

for(I = 0;我& ltLON;i++)

{gotoxy(28,I+3);

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

};

gotoxy(40,2);printf("the

長度:“);

for(I = 0;我& ltLON;i++)

{gotoxy(43,I+3);

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

};

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

for(I = 0;我& ltLON;i++)

{gotoxy(53,I+3);

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

};

平均值= 0;

for(I = 0;我& ltl;i++)

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

平均值=平均值/N;

gotoxy(10,23);

printf("ASL:ASL(%d)=%f ",N,平均值);

gotoxy(20,24);

printf("任意鍵通過");

ch 1 = getch();

};

如果(L & gt30)

{

clr SCR();

如果(LON & gt;L-30){ LON = L-30;};

gotoxy(1,1);printf(" HashTable:");

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

for(I = 0;我& ltLON;i++)

{gotoxy(1,I+3);

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

};

gotoxy(9,2);printf(" H(鍵)

是:“);

for(I = 0;我& ltLON;i++)

{gotoxy(10,I+3);

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

};

gotoxy(19,2);printf(" name:");

for(I = 0;我& ltLON;i++)

{gotoxy(19,3+I);

printf("%s ",hlist[i+30]。姓名);

};

gotoxy(28,2);printf(" spell:");

for(I = 0;我& ltLON;i++)

{gotoxy(28,I+3);

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

};

gotoxy(40,2);printf("the

長度:“);

for(I = 0;我& ltLON;i++)

{gotoxy(43,I+3);

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

};

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

for(I = 0;我& ltLON;i++)

{gotoxy(53,I+3);

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

};

平均值= 0;

for(I = 0;我& ltl;i++)

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

平均值=平均值/N;

gotoxy(10,23);

printf("ASL:ASL(%d)=%f ",N,平均值);

gotoxy(20,24);

printf("任意鍵通過");

ch 1 = getch();

};

如果(L & gt45)

{

clr SCR();

如果(LON & gt;L-45){ LON = L-45;};

gotoxy(1,1);printf(" HashTable:");

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

for(I = 0;我& ltLON;i++)

{gotoxy(1,I+3);

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

};

gotoxy(9,2);printf(" H(鍵)

是:“);

for(I = 0;我& ltLON;i++)

{gotoxy(10,I+3);

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

};

gotoxy(19,2);printf(" name:");

for(I = 0;我& ltLON;i++)

{gotoxy(19,3+I);

printf("%s ",hlist[i+45]。姓名);

};

gotoxy(28,2);printf(" spell:");

for(I = 0;我& ltLON;i++)

{gotoxy(28,I+3);

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

};

gotoxy(40,2);printf("the

長度:“);

for(I = 0;我& ltLON;i++)

{gotoxy(43,I+3);

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

};

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

for(I = 0;我& ltLON;i++)

{gotoxy(53,I+3);

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

};

平均值= 0;

for(I = 0;我& ltl;i++)

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

平均值=平均值/N;

gotoxy(10,23);

printf("ASL:ASL(%d)=%f ",N,平均值);

gotoxy(20,24);

printf("任意鍵通過!");

ch 1 = getch();

};

}

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

void main()

{ InP();/*輸入原始表*/

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

a:clr SCR();

gotoxy(21,2);

printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");

printf("歡迎使用!\ n ");

printf(" \ n ");

gotoxy(22,4);

printf("1。打印

哈希表。");

printf(" \ n ");

gotoxy(22,6);

printf(" 2 . research . ");

printf(" \ n ");

gotoxy(22,8);

printf("3。退出”);

printf(" \ n ");

gotoxy(22,12);

printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \ n ");

printf(" \ n您的選擇是:");

printf(" \ n ");

gotoxy(24,14);

ch 1 = getch();

if (ch1==0x33){

printf("感謝您的

使用”);printf(" \ n ");getch();exit();};/*"x ":退出*/

if(ch 1 = = 0x 31){ dhash();};/*表的屬性*/

if(ch 1 = = 0x 32){ findhlist();};/*查找*/

轉到a;

}