I need to convert the hash table from the below to one that inserts the name of the months of year and the number of days in each month into a Hash table of size four. I have to use the sum of the ASCII of three letters and mod them with 4.
class HASH
{
private:struct Node
{
int info;
Node* next;
};
Node *H[4];
public: HASH(){ for (int i = 0; i < 4; ++i)H[i] = NULL; }
int HashFunction(int x)
{
return x % 4;
}
void insert(int x)
{
int i = HashFunction(x);
//Inser xin H[i] linked list
Node *p = new Node; p->info = x;
p->next = H[i];
H[i] = p;
}
bool Search(int x)
{
int i = HashFunction(x);
Node *p = H[i];
while (p != NULL && p->info != x)
{
p = p->next;
}
if (p == NULL) return false;
else return true;
}
void DisplayHashTable()
{
Node *p;
for (int i = 0; i < 4; ++i)
{
p = H[i]; cout << "H[" << i << "]-->=";
while (p != NULL)
{
cout << p->info << " | ";
p = p->next;
}
cout << "NULL" << endl;
}
}
};
int main()
{
HASH h;
int a[10] = { 23, 15, 11, 34, 13, 21, 20, 44, 24, 14 };
for (int i = 0; i < 10; ++i)
h.insert(a[i]);
h.DisplayHashTable();
bool found = h.Search(44);
if (found == true) cout << "44 is found\n";
else cout << "44 does not exists\n"; cout << endl;
found = h.Search(55);
h.Search(55); cout << endl;
if (found == true) cout << "55 is found\n";
else cout << "55 does not exists\n"; cout << endl;