-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_HashMaps.cpp
More file actions
79 lines (77 loc) · 1.74 KB
/
8_HashMaps.cpp
File metadata and controls
79 lines (77 loc) · 1.74 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
77
78
79
#include <iostream>
#include<cstring>
using namespace std;
template <typename T>
class Node
{
public:
string key;
T value;
Node *next;
Node(string key, string val)
{
this->key = key;
value = val;
next = NULL;
}
};
template <typename T>
class hashtable
{
Node<T> **table; // this will be pointing to array of pointers i.e each pointer in this array points to a linked list of 'Node'
int table_size;
int current_size;
int hashFn(string key)
{
int idx = 0;
int p = 1;
for (int j = 0; j < key.length(); j++)
{
idx = idx + (key[j] * p) % table_size;
idx = idx % table_size;
p = (p * 27) % table_size;
}
return idx;
}
public:
hashtable(int ts)
{
table_size = ts;
current_size = 0;
table = new Node<T> *[table_size];
for (int i = 0; i < table_size; i++)
table[i] = NULL;
}
void insert(string key, T val)
{
int idx = hashFn(key);
Node<T> *n = new Node<T>(key,val);
n->next = table[idx];
table[idx] = n;
current_size++;
}
void print()
{
for (int i = 0; i < table_size - 1; i++)
{
Node<T> *temp = table[i];
while (temp != NULL)
{
cout << temp->key << "->";
temp = temp->next;
}
cout << endl;
}
}
};
int main()
{
hashtable<int> obj(7);
obj.insert("burger", 45);
obj.insert("maggi", 85);
obj.insert("pepsi", 35);
obj.insert("fries", 100);
obj.insert("thali", 335);
obj.print();
return 0;
}