-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashing.cpp
More file actions
76 lines (66 loc) · 1.04 KB
/
hashing.cpp
File metadata and controls
76 lines (66 loc) · 1.04 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
using namespace std;
template <class T>
class HashTable
{
T *arr;
int n, size, k;
int get_hash(T ele)
{
return (ele * ele) % k;
}
void clean()
{
for (int i = 0; i < n; i++)
{
arr[i] = -1;
}
}
public:
HashTable()
{
n = 20;
size = 0;
k = 7;
arr = new T[n];
clean();
}
HashTable(int n)
{
size = 0;
k = 7;
arr = new T[n];
clean();
}
void insert(T ele)
{
int pos = get_hash(ele);
while (arr[pos] != -1)
{
pos = (pos + 1) % n;
}
arr[pos] = ele;
}
int get_size()
{
return size;
}
void display()
{
cout << "\nHash Table:" << endl;
for (int i = 0; i < n; i++)
{
cout << arr[i] << endl;
}
}
};
int main()
{
HashTable<int> h;
h.insert(7);
h.insert(17);
h.insert(4);
h.insert(1);
h.display();
return 0;
}