Pagini recente » Cod sursa (job #2308212) | Cod sursa (job #1560423) | Cod sursa (job #3184477) | Cod sursa (job #610073) | Cod sursa (job #1611600)
#include <iostream>
#include <fstream>
#include <algorithm>
#define MOD 2000001
using namespace std;
struct Elem {
int value;
Elem* next;
};
class Hashmap {
private:
Elem* hashmap[MOD];
public:
Hashmap() {
for(int i = 0; i < MOD; ++i)
hashmap[i] = 0;
}
Elem* find(int value) {
Elem* curr = hashmap[value % MOD];
while (curr) {
if (curr->value == value) break;
curr = curr->next;
}
return curr;
}
void remove(int value) {
Elem* curr = hashmap[value % MOD];
Elem* prev = 0;
while (curr) {
if (curr->value == value) break;
prev = curr;
curr = curr->next;
}
if (curr) {
if (prev) prev->next = curr->next;
else hashmap[value % MOD] = curr->next;
delete curr;
}
}
void insert(int value) {
Elem* curr = hashmap[value % MOD];
Elem* newValue = new Elem;
newValue->value = value;
hashmap[value % MOD] = newValue;
if (curr) newValue->next = curr;
else newValue->next = 0;
}
};
int main() {
ifstream input;
ofstream output;
input.open("hashuri.in");
output.open("hashuri.out");
int n;
Hashmap *map = new Hashmap();
input >> n;
for(int i=0; i < n; ++i) {
int op, x;
input >> op >> x;
if (op == 1) {
map->insert(x);
} else if (op == 2) {
map->remove(x);
} else {
Elem* el = map->find(x);
if (el) output << 1;
else output << 0;
output << endl;
}
}
input.close();
output.close();
}