Hi can anyone help me write this program with comment statements so I can learn
ID: 3829577 • Letter: H
Question
Hi can anyone help me write this program with comment statements so I can learn how to build a program as such?
C++: Create a hash table class/struct.
Define an array that holds 27 elements.
Define a function called Hash(int)
-This function returns the modulo of that int by the size of the table (array).
Define an add function that takes an integer.
-This function takes the integer, determines the hash of that number by calling the above hash function, then adds it to the table using linear probing for collision resolution.
Define a function that looks up a value, it takes an integer, return -1 if the value is not in the table.
Create a main that allows the user to add and lookup items in the table.
Explanation / Answer
Here is the code
--------------------------------
#include <iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int TABLE_SIZE = 27;
class HashTable
{
public:
int table[TABLE_SIZE]; //Array for holding the hash table entries
HashTable()
{
for (int i=0; i<TABLE_SIZE; i++)
//Initializing all entries with -1 to mark the table is empty.
// Here -1 means that location in table is empty, so an item can be inserted.
table[i] = -1;
}
//Takes the item and returns its hash value.
int hash(int a)
{
return a%TABLE_SIZE;
}
//Takes the item and inserts in hash table
void add(int a)
{
int hsh = hash(a);//get the hash of the item.
int count =0;
//probe the table linearly untill we get an empty location or all the locations have been probed.
while (table[hsh] != -1 && count < TABLE_SIZE)
{
hsh = (hsh+1)%TABLE_SIZE;
count = count + 1;
}
if (table[hsh] == -1)
{
//found empty location, inserting item.
table[hsh] = a;
cout << "Successfully added the key to hash table.";
}
else
cout << "The hash table is full, no more insertions can be done.";
}
//Takes an item and returns same item if it is present in table else returns -1
int lookup(int a)
{
int hsh = hash(a);//get the hash of the item.
int count =0;
//probe the table linearly untill we get the item we are looking for
//or an empty location or all the locations have been probed.
while (table[hsh] != a && table[hsh] != -1 && count < TABLE_SIZE)
{
hsh = (hsh+1)%TABLE_SIZE;
count = count + 1;
}
if (table[hsh] == a)
{
//Found the key in the table
cout << "The key is found in the table";
return a;
}
else
{
cout << "The key is not found in the table.";
return -1;
}
}
};
int main()
{
HashTable hash;
int key, choice;
while (1)
{
cout<<" ----------------------"<<endl;
cout<<"Operations on Hash Table"<<endl;
cout<<" ----------------------"<<endl;
cout<<"1.Add item into the table"<<endl;
cout<<"2.Lookup item in the table"<<endl;
cout<<"3.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter element to be inserted: ";
cin>>key;
hash.add(key);
break;
case 2:
cout<<"Enter element to be looked up: ";
cin>>key;
hash.lookup(key);
break;
case 3:
exit(1);
default:
cout<<" Enter correct option ";
}
}
return 0;
}
Point to be noted:
--------------------------------------------
This code assumes that there is no delete operation in the hash table, otherwise the handling and code would be different.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.