-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHASHING.cpp
72 lines (48 loc) · 1.31 KB
/
HASHING.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
// .............Hashing is used in unordered set and unordered map also..............
#include<iostream>
#include<list>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
class Hashing{
vector<list<int>>hashtable;
int buckets;
public:
Hashing(int size){
buckets = size;
hashtable.resize(size);
}
int hashValue(int key){
return key%buckets; // division method
}
void addKey(int key) {
int idx = hashValue(key);
hashtable[idx].push_back(key);
}
list<int>::iterator searchKey(int key){
int idx = hashValue(key);
return find(hashtable[idx].begin(),hashtable[idx].end(),key);
}
void deleteKey(int key) {
int idx = hashValue(key);
auto it = searchKey(key);
if(it != hashtable[idx].end()){ // key is present....
hashtable[idx].erase(it);
cout<<key<<" is deleted"<<endl;
}
else{
cout<<"key is not present in the hashtable"<<endl;
}
}
};
int main() {
Hashing h(10);
h.addKey(5);
h.addKey(23);
h.addKey(123);
h.addKey(1);
h.deleteKey(23);
h.deleteKey(23);
return 0;
}