-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash_table.cpp
executable file
·112 lines (104 loc) · 2.21 KB
/
hash_table.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <bits/stdc++.h>
using namespace std;
bool in(string mystr, char mychar)
{
int count = 0;
for (int i = 0; i < mystr.length(); i++)
{
if (mychar == mystr[i])
{
count++;
}
}
return (count) ? true : false;
}
class HashTable
{
public:
string myarr[9];
HashTable()
{
for (int i = 0; i < 9; i++)
{
myarr[i] = "NULL";
}
}
int hashing(string newStr);
void insert(string newStr);
void deletehash(string newStr);
bool search(string newStr);
};
int HashTable::hashing(string newStr)
{
string number = "0123456789";
string vowel = "AEIOUaeiou";
int con_count = 0;
int digit_sum = 0;
for (int i = 0; i < newStr.length(); i++)
{
if (!in(vowel, newStr[i]) && !in(number, newStr[i]))
{
con_count++;
}
else if (in(number, newStr[i]))
{
digit_sum += (newStr[i] - '0');
}
}
return (con_count * 24 + digit_sum) % 9;
}
void HashTable::insert(string newStr)
{
int hash_index = hashing(newStr);
int i = 0;
while (myarr[hash_index] != "NULL")
{
hash_index = (hash_index + 1) % 9;
}
myarr[hash_index] = newStr;
}
bool HashTable::search(string newStr)
{
int k = hashing(newStr);
while (myarr[k] != newStr)
{
if (myarr[k] == "NULL")
{
return false;
}
k = (k + 1) % 9;
}
return myarr[k] == newStr;
}
void HashTable::deletehash(string newStr)
{
int k = hashing(newStr);
while (myarr[k] != newStr)
{
if (myarr[k] == "NULL")
{
return;
}
k = (k + 1) % 9;
}
if (myarr[k] == newStr)
{
myarr[k] = "NULL";
}
}
int main()
{
string newstr = "ST1E89B8A32";
string newstr2 = "Hi9IamTaj";
HashTable myHash;
myHash.insert(newstr);
myHash.insert(newstr);
myHash.insert(newstr2);
myHash.deletehash(newstr2);
cout << myHash.search("ABC") << endl;
for (int i = 0; i < 9; i++)
{
cout << myHash.myarr[i] << " ";
}
return 0;
}