forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem1.cpp
93 lines (70 loc) · 2.34 KB
/
Problem1.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
// Created by shazmaan on 7/1/2019.
//
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <bits/stdc++.h>
#define MAX 10
#define BuckSize 5
using namespace std;
class MyHashMap {
/** Initialize your data structure here. */
public:
int array[MAX][BuckSize];
MyHashMap() {
// std::fill_n(array[0][0], array[0][0] + sizeof(array), -1);
// std::fill_n(array,MAX,(-1));
memset(array,-1,sizeof(array));
}
void put(int key,int value);
int get(int key);
void remove(int key);
int hashfunc(int key);
void ReHash();
};
/** value will always be non-negative. */
void MyHashMap::put(int key, int value) {
int newKey = MyHashMap::hashfunc(key)%MAX; //find first index
int BuckKey = MyHashMap::hashfunc(newKey)%BuckSize;
if(array[newKey][BuckKey]!=(-1)){
array[newKey][BuckKey]=value;//rehash or update value
}else{
array[newKey][BuckKey]=value;
}
}
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
int MyHashMap::get(int key) {
int newKey = MyHashMap::hashfunc(key)%MAX; //find first index
int BuckKey = MyHashMap::hashfunc(newKey)%BuckSize;
return array[newKey][BuckKey];
// cout<<"KEY : "<<key<<" ";
}
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
void MyHashMap::remove(int key) {
int newKey = MyHashMap::hashfunc(key)%MAX; //find first index
int BuckKey = MyHashMap::hashfunc(newKey)%BuckSize;
if(array[newKey][BuckKey]!=(-1)){
array[newKey][BuckKey]=(-1);
}else{
cout<<"No such element in HashTable!\n";
}
}
void MyHashMap::ReHash(){
//rehash function
}
int MyHashMap::hashfunc(int key) { //HashFunction for main Array
return std::hash<int>()(key);
}
int main(){
MyHashMap hashMap;
hashMap.put(1, 1);
hashMap.put(2, 2);
cout<<hashMap.get(1)<<endl; // returns 1
cout<<hashMap.get(3)<<endl; // returns -1 (not found)
hashMap.put(2, 1); // update the existing value
cout<<hashMap.get(2)<<endl; // returns 1
hashMap.remove(2); // remove the mapping for 2
cout<<hashMap.get(2)<<endl; // returns -1 (not found)
}