Cod sursa(job #373364)

Utilizator titusuTitus C titusu Data 13 decembrie 2009 17:06:27
Problema Hashuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
using namespace std;
#include <cstdio>
#define P 30103

struct nod{
	int info;
	nod *next;
};

nod * hash[P+10];
int n;

int Apartine(int x){
	int poz=x%P;
	nod * p=hash[poz];
	while(p)
		if(p->info==x)
			return 1;
		else
			p=p->next;
	return 0;
}

void Adauga(int x){
	if(Apartine(x))
		return ;
	int poz=x%P;
	nod *p=new nod;
	p->info = x;
	p->next=hash[poz];
	hash[poz]=p;
}

void Sterge(int x){
	int poz=x%P;
	nod *p=hash[poz], *q;
	if(!p)
		return ;
	if(p->info==x){
		q=p;
		p=p->next;
		delete q;
	}
	else{
		while(p->next && p->next->info!=x)
			p=p->next;
		if(!(p->next))
			return;
		q=p->next;
		p->next=q->next;
		delete q;
	}
}

int main(){
	freopen("hashuri.in","r",stdin);
	freopen("hashuri.out","w",stdout);
	for(int i=0;i<P;++i)
		hash[i] = NULL;
	int nrop,x,op;
	scanf("%d",&nrop);
	for( ;  nrop ; --nrop){
		scanf("%d%d",&op,&x);
		switch(op){
			case 1:Adauga(x); break;
			case 2:Sterge(x); break;
			case 3:printf("%d\n",Apartine(x)); break;
		}
	}
	return 0;
}