forked from hiamsevval/Top-10-Frequent-Words
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashT.cpp
76 lines (69 loc) · 1.5 KB
/
hashT.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "hashT.h"
#include <iostream>
long long hashT::compute_hash(std::string const& str) //computes the mod value
{
int p = 31;
long long power_of_p = 1;
long long hash_val = 0;
for (int i = 0; i < str.length(); i++) {
hash_val
= (hash_val
+ (str[i] - 'a' + 1) * power_of_p)
% HTSize;
power_of_p
= (power_of_p * p) % HTSize;
}
return hash_val;
}
hashT::hashT() { //constuctor
HTSize = 4000;
for (int i = 0; i < HTSize; i++) {
hTable[i] = NULL;
}
}
hashT::~hashT() { //destructor
nodeType *temp;
nodeType *tempnext;
for (int i = 0; i < HTSize; i++) {
temp = hTable[i];
while (temp != NULL) {
tempnext = temp->link;
delete temp;
temp = tempnext;
}
hTable[i] = NULL;
}
HTSize = 0;
}
void hashT::insert(const std::string& word) {
nodeType *newNode, *current, *trailCurrent;
int index = compute_hash(word);
if (hTable[index] != NULL) {
current = hTable[index];
trailCurrent = current;
if (hTable[index]->info1 == word)
hTable[index]->info2++;
else {
while (current != NULL && current->info1 != word) {
current = current->link;
}
if (current == NULL) {
newNode = new nodeType;
newNode->info1 = word;
newNode->info2 = 1;
newNode->link = NULL;
trailCurrent->link = newNode;
}
else {
current->info2++;
}
}
}
else {
newNode = new nodeType;
newNode->info1 = word;
newNode->info2 = 1;
newNode->link = NULL;
hTable[index] = newNode;
}
}