-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashTableSeperateChaining.cpp
More file actions
56 lines (46 loc) · 1.13 KB
/
hashTableSeperateChaining.cpp
File metadata and controls
56 lines (46 loc) · 1.13 KB
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
#include<bits/stdc++.h>
using namespace std;
class SeperateChaining {
private:
int bucket;
vector< vector<int> > v;
public:
SeperateChaining(int n) {
// initialize
this->bucket = n;
v = vector< vector<int> >(n);
}
// hash function
int getHashIndex(int key) {
return key % bucket;
}
void insertItem(int x) {
v[getHashIndex(x)].push_back(x);
}
void deleteItem(int x) {
int index = getHashIndex(x);
for(int i=0;i<v[index].size();i++) {
if(v[index][i] == x) {
v[index].erase(v[index].begin() + i);
cout << x << " deleted!" << "\n";
return;
}
}
cout << "No element found!" << "\n";
}
void display() {
for(int i=0;i<v.size();i++) {
cout << i;
for(int j=0;j<v[i].size();j++)
cout << " ->" << v[i][j];
cout << "\n";
}
}
};
int main() {
vector<int> v{12,3,23,4,11,32,26,33,17,19};
SeperateChaining sc(10);
for(int i=0;i<10;i++)
sc.insertItem(v[i]);
sc.display();
}