I have a hash table program shown below
#include<bits/stdc++.h>
#include <algorithm>
std::string fileName;
std::fstream readFile;
const int arraySize = 100;
int storeFile[arraySize];
class Hash
{
std::list<int> *table;
std::list<int>::iterator anItetator;
public:
int count = 0;
int mod = 100;
Hash();
Hash(int Value);
void insertItem(int key);
int hashFunction(int key) {
return (key % mod);
}
void loopHash();
void displayHash();
};
Hash::Hash(){
}
Hash::Hash(int b)
{
this->mod = b;
table = new std::list<int>[mod];
}
void Hash::insertItem(int key)
{
int index = hashFunction(key);
table[index].push_back(key);
}
void Hash::displayHash() {
for (int i = 0; i < mod; i++) {
std::cout << i;
for (auto x : table[i])
std::cout << " --> " << x;
std::cout << std::endl;
}
}
void Hash::loopHash() {
for(anItetator = table->begin(); anItetator != table->end(); ++anItetator){
if(table->empty()==true) {
count++;
}
std::cout << "Total number of empty entries is: " << count;
std::cout << "The largest chain is: " << *std::max_element(table->begin(),table->end());
}
}
int main(int argc, char *argv[])
{
int n = arraySize;
Hash h(h.mod);
std::cout << "Please enter the name of the file: " << std::endl;
std::cin >> argv[0];
fileName = argv[0];
std::cout << "Attempting to read file " << fileName << std::endl;
readFile.open(fileName);
for(int i = 0; i < n; i++) {
while (readFile >> storeFile[i])
{
h.insertItem(storeFile[i]);
}
}
//h.displayHash();
h.loopHash();
readFile.close();
return 0;
}
However theres specific information i need from the results that im not quite sure how to find. These are 2 outputs
- The number of empty elements in the hash table
- The length of the longest chain
Ive attempted to do something like this with my void Hash::loopHash()
however this function prints no values.
If anyone knows about hash-tables and can help me with this that would be great.