-
Notifications
You must be signed in to change notification settings - Fork 46
/
chainedHash.cpp
66 lines (62 loc) · 1.21 KB
/
chainedHash.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
#include <iostream>
#include <list>
using namespace std;
class Hash
{
public:
int Buckets;
list<int> *Table;
list<int>::iterator i;
Hash(int key)
{
this->Buckets = key;
Table = new list<int>[Buckets];
}
int getHash(int key)
{
return key % Buckets;
}
void insertHash(int key)
{
int index = getHash(key);
cout << index << " ";
Table[index].push_back(key);
}
void displayBuckets()
{
for (int t = 0; t < Buckets; t++)
{
for (auto x : Table[t])
{
cout << " --> " << x << " ";
}
cout << '\n';
}
}
void deleteHash(int key)
{
int index = getHash(key);
for (i = Table[index].begin(); i != Table[index].end(); i++)
{
if (*i == key)
{
Table[index].erase(i);
break;
}
}
}
};
int main()
{
Hash h(5);
h.insertHash(123);
h.insertHash(312);
h.insertHash(312);
h.insertHash(321);
h.insertHash(321);
h.insertHash(321);
h.insertHash(321);
h.deleteHash(321);
h.deleteHash(321);
h.displayBuckets();
}