-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash.cpp
More file actions
37 lines (33 loc) · 983 Bytes
/
Hash.cpp
File metadata and controls
37 lines (33 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "Hash.h"
int HashTable::hashfunc(const std::string& input)
{
int ascii_sum = 0;
for (char ch : input)
ascii_sum += ch;
int ind = ascii_sum % 1000;
while (hashtable[ind].getSymbol() != std::string())
{
if (hashtable[ind].getSymbol() == input) break;
else ++ind;
if (ind == SIZE_OF_HASH_TABLE) ind = 1;
}
return -ind;
}
void HashTable::hashinsert(const std::string & input)
{
int ind = -hashfunc(input);
if (hashtable[ind].getSymbol() != std::string()) return;
else hashtable[-hashfunc(input)].setSymbol(input);
}
void HashTable::printTable(void)
{
std::cout << "Hash Table : \n| Hash Value | Symbol | Link |\n";
for (int i = 0; i < 37; ++i) printf("-"); std::cout << std::endl;
std::string temp;
for (int i = 0; i < SIZE_OF_HASH_TABLE; ++i)
{
if ((temp = hashtable[i].getSymbol()) != std::string())
std::cout << "|" << std::setw(11) << (-i) << " |" << std::setw(11) << temp << " | NIL |" << std::endl;
}
std::cout << std::endl;
}