write functions to do the following. Find the size of the hash table write a fun
ID: 3915293 • Letter: W
Question
write functions to do the following. Find the size of the hash table write a function that will print all of the elements and their locations in the hash table. write a function to Remove the second element from the hash table and print it. The table is otherwise left unchanged. write a subprogram that will find the true load factor. write a function that will insert a new value into a hash table. write a function that will print the empty locations in the hash table. write a function that will create a new hash table by entering the data in reverse order. write a function to retrieve a value from a hash table. write a function to determine whether the two hash tables are equal. write a function that determines whether a sport is empty or full. write a function to implement random probing using c++
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
const int TABLE_SIZE = 128;
class HashEntry
{
public:
int key;
int value;
HashEntry(int key, int value)
{
this->key = key;
this->value = value;
}
};
class HashTable
{
private:
HashEntry **table;
public:
HashTable()
{
table = new HashEntry * [TABLE_SIZE];
for (int i = 0; i< TABLE_SIZE; i++)
{
table[i] = NULL;
}
}
void HashSize()
{
std::cout <<"Hash Table size :" << TABLE_SIZE<< std::endl;;
}
void Insert(int key, int value)
{
int hash = key % TABLE_SIZE;
while (table[hash] != NULL && table[hash]->key != key)
{
hash =(hash + 1) % TABLE_SIZE;
}
if (table[hash] != NULL)
delete table[hash];
table[hash] = new HashEntry(key, value);
}
void PrintAll()
{
for(int i=0;i <TABLE_SIZE;i++)
std::cout <<"Location :"<<table[i]->key << " element :"<<table[i]->value<< std::endl;
}
void Remove(int key)
{
int hash =key % TABLE_SIZE;
while (table[hash] != NULL)
{
if (table[hash]->key == key)
break;
hash = (hash + 1) % TABLE_SIZE;
}
if (table[hash] == NULL)
{
cout<<"No Element found at key "<<key<<endl;
return;
}
else
{
delete table[hash];
}
cout<<"Element Deleted"<<endl;
}
~HashTable()
{
for (int i = 0; i < TABLE_SIZE; i++)
{
if (table[i] != NULL)
delete table[i];
delete[] table;
}
}
};
int main()
{
HashTable obj;
obj.Insert(1,1234);
obj.Insert(2,5345);
std::cout << "Insert Successfully" << std::endl;
obj.Remove(2);
obj.HashSize();
obj.PrintAll();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.