Question: Adjust this program to demonstrate quadratic hash probing when a collision occurs. #include #include #include #include using namespace std; const int TABLE_SIZE = 29; class
Adjust this program to demonstrate quadratic hash probing when a collision occurs.
#include
#include
#include
#include
using namespace std;
const int TABLE_SIZE = 29;
class HashEntry
{
public:
int value;
public:
HashEntry(int value)
{
this->value = value;
}
int GetValue()
{
return value;
}
};
class HashMap
{
public:
HashEntry **table;
public:
HashMap()
{
table = new HashEntry*[TABLE_SIZE];
for(int i=0;i { table[i]=NULL; } } //Destructor to Deallocate the Memory Allocated Each and Every Key value Pair public: ~HashMap() { for(int i=0;i { if (table[i] != NULL) delete table[i]; delete[] table; } } public: // Generates the hash for this table int HashFunc(int value) { return value % TABLE_SIZE; } public: void Insert(int value)//Insert the Item { int hash = HashFunc(value); while (table[hash] != NULL && table[hash]->GetValue() != value) { hash=HashFunc(hash+1); } if(table[hash]!=NULL) delete table[hash]; table[hash] = new HashEntry(value); } public: int Search(int value) { int hash = HashFunc(value); while (table[hash] != NULL && table[hash]->GetValue() != value) { hash = HashFunc(hash + 1); } if (table[hash] == NULL) return -1; else return hash; } // Removes a specified value from the table, if the value is found public: void Remove(int value) { int hash = HashFunc(value); while (table[hash] != NULL) { if (table[hash]->GetValue() == value) break; hash=HashFunc(hash + 1); } if(table[hash]==NULL) { cout<<" Element "< return; } else { delete table[hash]; } cout<<"Element Deleted Successfully "; } public: void print() { for(int i=0; i < TABLE_SIZE; i++) { if (table[i] != NULL) cout<< table[i]->GetValue()<<"->"; } cout< } }; int main() { HashMap obj; int value; int temp_key; int option; while (1) { cout<<" 1.Insert key into the table"< cout<<" 2.Search key from the value"< cout<<" 3.Delete key at a value"< cout<<" 4.Print elements in hashTable"< cout<<" 5.Exit"< cout<<" Please Select your choice:"< cin>>option; switch(option) { case 1: cout<<"Enter element to be inserted: "; cin>>value; temp_key = obj.HashFunc(value); cout<< value << " has been inserted at the Index of: "< obj.Insert(value); break; case 2: cout<<"Enter value of the element to be searched: "; cin>>value; if (obj.Search(value) == -1) { cout<<"The element "< continue; } else { cout<<"Element found at index: "; cout< } break; case 3: cout<<"Enter value of the element to be deleted: "; cin>>value; obj.Remove(value); break; case 4: cout << "The current table is:"< obj.print(); break; case 5: exit(1); default: cout<<" Invalid Option please Enter correct option"< } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
