Pagini recente » Cod sursa (job #2151530) | Cod sursa (job #939867) | Cod sursa (job #383031) | Cod sursa (job #953626) | Cod sursa (job #530595)
Cod sursa(job #530595)
#include<cstdio>
#include<fstream>
using namespace std;
#define PRIME 666013
struct hashElem{
int value;
hashElem *next;
};
hashElem *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);
hashElem *aux = new hashElem();
aux->value = val;
aux->next = v[poz];
v[poz] = aux;
}
void removeHash(int val) {
int poz = h(val);
hashElem *aux = v[poz];
if (v[poz]){
if (v[poz]->value == val) v[poz] = v[poz]->next;
else {
hashElem *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);
hashElem *aux = v[poz];
while (aux!=NULL) {
if (aux->value == val) return 1;
aux = aux->next;
}
return 0;
}
int main() {
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
initHash();
fin>>N;
int i, op, val;
for(i = 0; i < N; i++) {
fin>>op>>val;
if (op == 1) addHash(val);
else if (op == 2) removeHash(val);
else fout<<isInHash(val)<<"\n";
}
return 0;
}