Current location - Training Enrollment Network - Books and materials - Using Hash Table to Find Books
Using Hash Table to Find Books
# include & ltiostream & gt

# include & lt string & gt

Use namespace std

#define HASH_LENGTH 50 // Length of hash table

# Define M 47 // random number

# Define Name _ Number 30 // Name Quantity

Typedef structure

{char * py// Pinyin of the name

int k; //Integer corresponding to Pinyin

} name;

NAME NAME list[HASH _ LENGTH]; //Global variable name

Typedef structure//hash table

{char * py// Pinyin of the name

int k; //Integer corresponding to Pinyin

Int si// search length

} hash;

HASH HashList[HASH _ LENGTH]; //global variable hash

Void InitNameList() // name (structure array) initialization

{ char * f;

int r,s0,I;

for(I = 0; I < hash length; i++)//★

{//★

List [i]. py = new char[64]; //★

List [i]. py[0]= 0; //★

}//★

Strcpy (name list [0]). Py, Jie Bao); //★

Strcpy (name list [1]). Py, "Cheng Gaoyang"); //★

Strcpy (name list [2]). Py, Chen Guangzhong); //★

Strcpy (name list [3]). Py, Chen Liangliang); //★

Strcpy (name list [4]). Py, Chen Yongzhou); //★

Strcpy (name list [5]). Py, "Phoenix Nest"); //★

Strcpy (name list [6]). Py, "Ge Xiangfeng"); //★

Strcpy (name list [7]). Py, "Hu Ting"); //★

Strcpy (name list [8]). Py, "Yellow Gold"); //★

Strcpy (name list [9]). Py, "Jiang Xiaojia"); //★

Strcpy (name list [10]). Py, "Laidong Street"); //★

Strcpy (name list [1 1]). Py, "Li Yexiao"); //★

Strcpy (name list [12]). Py, "Methodist Association"); //★

Strcpy (name list [13]). Py, Li Jue); //★

Strcpy (name list [14]). Py, "Li Zhuoqun"); //★

Strcpy (name list [15]). Py, "Lin Fujun"); //★

Strcpy (name list [16]). Py, Robin); //★

Strcpy (name list [17]). Py, "Luo Keqing"); //★

Strcpy (name list [18]). Py, Ni Chao); //★

Strcpy (name list [19]). Py, Pan Huafeng); //★

List [20]. Py, "Four Kings"); //★

Strcpy (name list [2 1]). Py, "Song Zhanhui"); //★

List [22]. Py, "Sun Zhengqing"); //★

List [23]. Py, Wang Haofeng); //★

List [24]. Py, "Wang Junshuai"); //★

List [25]. Py, "Wang Qinde"); //★

List [26]. Py, Wang Zejun); //★

List [27]. Py, Wang Keke); //★

List [28]. Py, Wei Xing); //★

List [29]. Py, "Wu Renke"); //★

for(I = 0; I & ltNAME _ NOi++)

{

s0 = 0;

F = name list [i]. py;

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

/* Method: Add the ASCII codes corresponding to each character in the string to get the integer as the key word of the hash table */

s0 = *(f+r)+s0;

List [i]. k = s0

}

}

Void CreateHashList() // Create a hash table.

{

int I;

for(I = 0; I < hash length; i++)

{

HashList[i]。 py = new char[64]; //★

HashList[i]。 py[0]= 0; //★

HashList[i]。 k = 0;

HashList[i]。 si = 0;

}

for(I = 0; I < hash length; i++)

{

int sum = 0;

int adr=(NameList[i]。 k)% M;

//Hash function

int d = adr

If(HashList[adr].si==0) // If there is no conflict,

{

HashList[adr]。 K = name list [i]. k;

HashList[adr]。 py=NameList[i]。 py;

HashList[adr]。 si = 1;

}

Else // conflict

{

while (HashList[d].k! =0)

{

D =(d+ name list [i]. k % 10+ 1)% M; //Pseudo-random detection rehash method to deal with conflicts.

sum = sum+ 1; //Search times plus 1

};

HashList[d]。 K = name list [i]. k;

HashList[d]。 py=NameList[i]。 py;

HashList[d]。 si = sum+ 1;

}

}

}

Void FindList() // search

{

String name;

int s0=0,r,sum= 1,adr,d;

Cout & lt& lt "Please enter the pinyin of your name:"

CIN & gt; & gt name; ;

for(r = 0; r & lt20; R++) // Find the integer (keyword) corresponding to the name pinyin.

s0+= name[r];

ADR = s0 % M; //Use hash function

d = adr

If(HashList[adr].k==s0) // is judged in three cases.

Cout & lt& lt "Name:"

else if (HashList[adr].k==0)

Cout & lt& lt "There is no such record!" & lt& ltendl

other

{

int g = 0;

while(g==0)

{

d =(d+s0 % 10+ 1)% M; //Pseudo-random detection rehash method to deal with conflicts.

sum = sum+ 1;

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

{

Cout & lt& lt "There is no such record!" & lt& ltendl

g = 1;

}

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

{

Cout & lt& lt "Name:"

g = 1;

}

};

}

}

Void Display() // display hash table

{

int I;

Floating average = 0;

Cout & lt& lt”\ nAddress \ keyword \ t \ t \ search length \ th (key) \ tname \ n "; //Display format

for(I = 0; I & lt50; i++)

{

Cout & lt& lt I<& lt "";

cout & lt& lt" \ t " & lt& ltHashList[i]。 k & lt& lt" ";

cout & lt& lt" \ t \ t " & lt& ltHashList[i]。 si & lt& lt" ";

cout & lt& lt" \ t \ t " & lt& lt(HashList[i]。 k % M)& lt; & lt" ";

cout & lt& lt" \ t " & lt& ltHashList[i]。 py & lt& lt" ";

cout & lt& lt”\ n”;

}

for(I = 0; I < hash length; i++)

average+=HashList[i]。 si;

average/= NAME _ NO;

Cout & lt& lt "Average search length: American sign language ("

}

int main()

{

char x;

init name list();

create hashlist();

Cout & lt& ltd displays hash table F. Find any key to exit, please select.

And (CIN >; & gtx)

{

if(x=='d ')

{

Display ();

cout & lt& ltendl

}

else if(x=='f ')

{

FindList();

cout & lt& ltendl

}

else break

}

for(int I = 0; I < hash length; i++)//★

{

Free (list [i]. py); //★

free(HashList[i].py); //★

}//★

Returns 0;

}