Home Contact Study Material Jaipur Photo UGC NET Computer Dictionary OS Data Structre Sorting algorithm Blog Favorite Links linux springsocial Research Research Group hadoop assembly programing C Prgming DSA Program Compiler Construction python Virtual Box ccna Ghazals or Songs Blank My Photos



SYMBOL TABLE IMPLEMENTATION using HASH TABLE (separate linked list for each hash value)

Programming Language to use:  C or Java

 

It is required to store various strings in a Symbol  Table. Assume  a HASH TABLE implementation for the Symbol Table and the hash function is  defined as follows:

 

Read in strings from an input file (text file)  and calculate  hash value for each string  using the hash function given below. You can permit collisions, in case if they occur [i.e. one or more strings can map to the same hash value; you can store them in the same sub-list corresponding to the computed hash value] Assume that the input string has English alphabets (upper case and lower case) and digits. Note the range of ASCII values for A-Z is 65-90, a-z is 97-122 and digits 0-9 is 48-57.

HASH FUNCTION for an input string is defined as follows:

HASH VALUE=

 ( (sum of ASCII values of  the English alphabets +  5*sum of ASCII values of digits present in the string) * 17 + 5 MOD 9

Example  :   Input String :  Az9

                     Hash Value = ((65 + 122 + 5*57) *17 + 5) mod 9= (472*17 +5) %7=

8029 % 9 = 1

                     Note: MOD denotes modulus operator [i.e. remainder after division]

 

Compute the hash values for each of the following twenty input strings:

 

   B2y   E3x   F4w  C5v   D2u   A2t  K5y M6z  N7a  Y3w

   b2Y   e3X   f4W  c5V   d2U   a2T  k5Y m6Z  n7A  y3W

 

Note: you can read each input string from a text file [one string in each line]

 

Display the contents of the Symbol Table showing the elements of each sub-list.  

 

Solution:-

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;

public class SymbolTable {

public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));

int n = Integer.parseInt(inp.readLine()); // input size

ArrayList<LinkedList<String>> symbolTable = new ArrayList<>();

// Initialize the symbol table
for(int i = 0; i < 7; i++)
symbolTable.add(new LinkedList<>());

// read input
for(int i = 0; i < n; i++){
String value = inp.readLine();

int sum = 0;

for(int j = 0; j < value.length(); j++)
sum += wt(value.charAt(j));

//System.out.println(sum);

//System.out.println(sum*15 + 5);

int index = (sum*15 + 5) % 7;

symbolTable.get(index).add(value);
}

int key = -1;

for(LinkedList<String > linkL : symbolTable){
key++;
if(linkL.size() == 0)
continue;

System.out.print("Hash Value = " + key + " : ");

for(String val : linkL)
System.out.print(val + "/");

System.out.println();
}

}

private static int wt(char c){
if(c >= '0' && c <= '9')
return 3 * c;
else
return c;
}
}