560 Programming and Data Structures
{
midassqu%10;
squ=squ/10;
}
return mid ;
void disp(int fa rr [] , int count)
{
int al;
for (a l= l; al<=count; al++)
printf ("%5d" , farr [ail]);
QLOTUI:
The numbers are as follows:
21 29 27 12 11 64 0
The Hash Table with Mid-Square method is as follows:
Index 1:
Index 2: 27 11
Index 3:
Index 4: 21 29 12
Explanation In the above program the main array 'msarr[]' having six elements is declared. In
addition to the above, /ml[]'/m 2[]'/m3[]' and 'm4[] are initialized . These arrays are initialized for
storing the index. Two functions are declared, squ mid () ; and d is (); . These two functions are
initialized for finding the middle digit of the square and display the keys. By using the mod operator,
the middle digit of the square number is calculated. After finding, the middle digit they are displayed
in the hash table.
The folding method: In the folding method, the key is divided into a number of parts. The parts, of
course, are dependent on the length of the key All the parts are added together. If the carry results from
this operation it is to be ignored and the number that remains behind after ignoring the carry is the
address of the key. By using this method, we fold the key. Hence, this method is called folding method.
For example, consider the following cases.
1) k = 879987: By using this method, we divide it in two parts like 879 and 987. They are added,
carry is ignored and the number after ignoring the carry becomes the address of the key. 879 +987
= 1866. Its most significant digit is 1. Hence, after ignoring l,the number that is left is 687 which
is the address of the key.
2) k=678897678 In this key is divided into the three parts. They are 678,897 and 678. By adding,
678+897+678= 2253. After ignoring last carry 2, the number that is left is 253. Therefore, 253 is the
address of the key Thus, the folding method is useful in converting a multiword key in to a single
word. By applying the hashing function, the key can be identified. The program can be developed
by the reader.