-
Notifications
You must be signed in to change notification settings - Fork 0
/
customMap.cpp
68 lines (62 loc) · 1.46 KB
/
customMap.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
/**
* customMap.cpp
* CustomMap class has implementation of hash table
*
* @author Olga Kuriatnyk
*/
#include "customMap.h"
// creating an empty table
CustomMap::CustomMap() {
for (auto &i : array) {
i = nullptr;
}
}
// destructor
CustomMap::~CustomMap() {
for (auto &i : array) {
if (i != nullptr) {
CustomerList *x = i;
while (i != nullptr) {
i = i->next;
delete x->obj;
delete x;
x = nullptr;
x = i;
}
}
}
}
// hash function for finding an index based on customerID
int CustomMap::hashFunction(int id) { return id % HASH_GROUPS; }
// get customer object by ID
Customer *CustomMap::getCustomerByID(int id) {
int position = hashFunction(id);
if (array[position] != nullptr) {
CustomerList *curr = array[position];
while (curr != nullptr) {
if (curr->val == id) {
return curr->obj;
}
curr = curr->next;
}
}
return nullptr;
}
// function for adding a customer to the hashtable
void CustomMap::addCustomer(int id, Customer *user) {
int position = hashFunction(id);
auto *placeholder = new CustomerList;
placeholder->val = id;
placeholder->obj = user;
placeholder->next = nullptr;
if (array[position] != nullptr) {
CustomerList *curr = array[position];
while (curr->next != nullptr) {
curr = curr->next;
}
curr->next = placeholder;
} else {
placeholder->next = array[position];
array[position] = placeholder;
}
}