Cod sursa(job #530595)

Utilizator Gaby_mMititelu Gabriel Gaby_m Data 7 februarie 2011 23:55:57
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#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;
}