-
Notifications
You must be signed in to change notification settings - Fork 1
/
HashTable.h
100 lines (91 loc) · 1.52 KB
/
HashTable.h
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <string>
using namespace std;
const int prime = 98317;
class HashNode
{
public:
HashNode* next;
string data;
bool active_state;
HashNode()
{
this->next = NULL;
data = "";
active_state = true;
}
};
class HashTable
{
public:
HashNode* table[prime];
HashTable()
{
for(int i = 0; i < prime; i++)
{
this->table[i] = NULL;
}
}
int loc(string s)
{
int loc = 1;
for(int i = 0; i < s.size(); i++)
{
loc = (loc*int(s[i]))%prime;
}
return loc;
}
void add(string s)
{
int pos = loc(s);
HashNode* current = this->table[pos];
if(current == NULL)
{
this->table[pos] = new HashNode();
this->table[pos]->data = s;
return;
}
while(current->next != NULL)
{
if(current->data == s and current->active_state == false)
{
current->active_state = true;
return;
}
current = current->next;
}
current->next = new HashNode();
current->next->data = s;
return;
}
void deactive(string s)
{
int pos = loc(s);
HashNode* current = this->table[pos];
while(current != NULL)
{
if(current->data == s)
{
current->active_state = false;
return;
}
current = current->next;
}
cout<<"INTERNAL ERROR !!! REMOVING NOT EXISTING NAME FROM NAMETABLE"<<endl;
return;
}
bool registered_check(string s)
{
int pos = loc(s);
HashNode* current = this->table[pos];
while(current != NULL)
{
if(current->data == s and current->active_state == true)
{
return true;
}
current = current->next;
}
return false;
}
};