-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhashmap.cpp
68 lines (59 loc) · 1.36 KB
/
hashmap.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
#include "hashmap.h"
HashMap::HashMap() : size(DEFAULT_MAP_SIZE)
{
headList = new struct hashNode[size];
}
HashMap::HashMap(int _size) : size(_size)
{
headList = new struct hashNode[size];
}
HashMap::~HashMap(){
for(int i = 0; i < size; i++){
delete headList[i].next;
}
delete [] headList;
}
int HashMap::hashAddr(char key){
return key % 10;
}
struct hashNode* HashMap::find(char key){
int addr = hashAddr(key);
struct hashNode* ptr = headList[addr].next;
while(ptr != nullptr){
if(ptr->key == key)
return ptr;
ptr = ptr->next;
}
return ptr;
}
bool HashMap::insert(char key, int val){
struct hashNode* ptr = find(key);
if(ptr == nullptr){
int addr = hashAddr(key);
struct hashNode* newNode = new struct hashNode(key, val);
newNode->next = headList[addr].next;
headList[addr].next = newNode;
return true;
}
else
return false;
}
int HashMap::getVal(char key){
struct hashNode* ptr = find(key);
if(ptr == nullptr)
return -1;
else
return ptr->val;
}
int HashMap::operator[](char key){
return getVal(key);
}
void initOpMap(){
opMap.insert('+', 0);
opMap.insert('-', 0);
opMap.insert('*', 1);
opMap.insert('/', 1);
opMap.insert('%', 1);
opMap.insert('^', 2);
opMap.insert('.', 3);
}