-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q2.cpp
170 lines (157 loc) · 3.84 KB
/
Q2.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#define SIZE 10
unsigned int prime[3] = {7,5,3}; // 2^7-1, 2^5-1, 2^3-1
template <class Tk,class Tv>
struct mapnode {
Tk key;
Tv val;
mapnode<Tk,Tv> *next = NULL;
};
template <class Tk,class Tv>
class UnordMap {
public:
mapnode<Tk,Tv> *hash_tab[SIZE] = {};
int hashIndex(Tk k) {
string ks;
ostringstream oss;
oss << k;
ks = oss.str();
unsigned long long int h = 0;
for(int i=0,j=0;i<ks.length();i++,j++) {
if(j>=3)
j=0;
h += ks[i] << prime[j];
h -= ks[i];
}
return h%SIZE;
}
void insert(Tk k, Tv v) {
int indx = hashIndex(k);
mapnode<Tk,Tv> *mnode = new mapnode<Tk,Tv>;
mnode->key = k;
mnode->val = v;
mnode->next = NULL;
mapnode<Tk,Tv> *p;
if(!hash_tab[indx]) {
hash_tab[indx] = mnode;
}
else {
p = hash_tab[indx];
while(p->next!=NULL) {
if(p->key==k)
return;
p=p->next;
}
if(p->key!=k) {
p->next = mnode;
}
}
}
void erase(Tk k) {
int indx = hashIndex(k);
mapnode<Tk,Tv> *p,*q;
if(hash_tab[indx]!=NULL) {
if(hash_tab[indx]->key==k) {
hash_tab[indx] = hash_tab[indx]->next;
return;
}
p = hash_tab[indx]->next;
q = hash_tab[indx];
while(p!=NULL) {
if(p->key==k) {
q->next = p->next;
delete p;
return;
}
q = p;
p=p->next;
}
}
}
mapnode<Tk,Tv> *search(Tk k) {
int indx = hashIndex(k);
mapnode<Tk,Tv> *p;
if(hash_tab[indx]!=NULL) {
p = hash_tab[indx];
while(p!=NULL) {
if(p->key==k) {
return p;
}
p = p->next;
}
}
return NULL;
}
bool find(Tk k) {
mapnode<Tk,Tv> *p = search(k);
if(p!=NULL)
return true;
return false;
}
void display() {
mapnode<Tk,Tv> *p;
for(int i=0;i<SIZE;i++) {
p = hash_tab[i];
while(p!=NULL) {
cout << "[" << i << "]" << p->key << "->" << p->val << " ";
p = p->next;
}
}
}
Tv& operator[](Tk k) {
mapnode<Tk,Tv> *p = search(k);
if(p==NULL) {
Tv df{};
insert(k,df);
p = search(k);
}
return p->val;
}
};
int main() {
UnordMap<int,int> map;
int n,a;
cin >> n;
int k,v;
while(n--) {
cin >> a;
// cout << "(" << a << ") ";
switch(a) {
case 1: //insert
cin >> k >> v;
// for(int i=1;i<=200;i++)
map.insert(k,v);
map.display();
cout << endl;
break;
case 2: //erase
cin >> k;
map.erase(k);
map.display();
cout << endl;
break;
case 3: //find
cin >> k;
cout << map.find(k) << endl;
break;
case 4: //[i]
cin >> k;
cout << map[k] << endl;
break;
case 5: //[i]=v
cin >> k >> v;
map[k] = v;
map.display();
cout << endl;
break;
case 6: //display
map.display();
cout << endl;
break;
}
}
return 0;
}