/*
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
... [Show More] 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.
*/
#include
#include
using namespace std;
// Declare the struct
struct element
{
int info;
element* next;
};
// the size of the hash table followed by a linked list
struct HashTable
{
private:
const static int Tablesize = 27; // Define an array that holds 27
elements.
element* ElementArea[Tablesize]; // elements in the hash table
public:
void initialization();
unsigned int Hash(int);
void add(int);
int search(int);
};
void HashTable :: initialization()
{
for(int i = 0; i < Tablesize; i++)
{
ElementArea[i] = new element;
ElementArea[i]->info = -999; // represents that the element area is
empty
ElementArea[i]->next = NULL;
}
}
// hash function, returns an unsigned integer
unsigned int HashTable:: Hash(int a)
{
return a % Tablesize;
}
void HashTable :: add(int inputvalue)
This study source was downloaded by 100000823950634 from CourseHero.com on 04-22-2021 08:51:01 GMT -05:00
https://www.coursehero.com/file/16658368/Source-Hash-Tablecpp/
This study resource was
shared via CourseHero.com
{
//This is where the chaining solution should be taken
unsigned int hashValue = Hash(inputvalue);
cout<< "The integer " << inputvalue << " hashes to " << hashValue << endl;
if (ElementArea[hashValue]->info == -999)
{
ElementArea[hashValue]->info = inputvalue;
}
else
{
element* temp = ElementArea[hashValue];
element* n = new element;
n->info = inputvalue;
n->next = NULL;
while(temp->next != NULL)
{
temp = temp->next; [Show Less]