Pagini recente » Cod sursa (job #2650101) | Cod sursa (job #3250556) | Cod sursa (job #505233) | Cod sursa (job #1890269) | Cod sursa (job #530589)
Cod sursa(job #530589)
#include<cstdio>
using namespace std;
#define PRIME 666013
struct hash{
int value;
hash *next;
};
hash *v[PRIME];
int N;
int h(int nr) {
return nr % PRIME;
}
void initHash() {
for (int i = 0; i < PRIME; i++)
v[i] = NULL;
}
void addHash(int val) {
int poz = h(val);
hash *aux = new hash();
aux->value = val;
aux->next = v[poz];
v[poz] = aux;
}
void removeHash(int val) {
int poz = h(val);
hash *aux = v[poz];
if (v[poz]){
if (v[poz]->value == val) v[poz] = v[poz]->next;
else {
hash *prev = aux;
aux = aux->next;
while (aux->value != val || aux!=NULL) {
prev = aux;
aux = aux->next;
}
if (aux) prev->next = aux->next;
}
}
}
int isInHash(int val) {
int poz = h(val);
hash *aux = v[poz];
while (aux!=NULL) {
if (aux->value == val) return 1;
aux = aux->next;
}
return 0;
}
int main() {
freopen("hashuri.in", "r", stdin);
freopen("hashuri.out", "w", stdout);
initHash();
scanf("%d", &N);
int i, op, val;
for(i = 0; i < N; i++) {
scanf("%d %d", &op, &val);
if (op == 1) addHash(val);
else if (op == 2) removeHash(val);
else printf("%d\n",isInHash(val));
}
return 0;
}