Cod sursa(job #714630)

Utilizator d.andreiDiaconeasa Andrei d.andrei Data 15 martie 2012 22:03:20
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <cstdio>

#define file_in "hashuri.in"
#define file_out "hashuri.out"

#define mod (666013)

class nod{
	
public:
	int val;
	nod * urm;
};


class Hash{
	
private:
	
	nod * H[mod+20];
  
public:
	
	//Hash();
	int cauta(int x);
	void adauga(int x);
	void sterge(int x);
	//~Hash();
};

Hash h;
int Q,Tip,x;

int Hash::cauta(int x){
	
	nod * p=H[x%mod];
	nod * c=p;
	while(c){
		if (c->val==x)
			return 1;
		c=c->urm;
	}
	
	return 0;
}

void Hash::adauga(int x){
	
	nod * p=H[x%mod];
	nod * c;
	c=new nod;
	c->val=x;
	c->urm=p;
	p=c;
}

void Hash::sterge(int x){
	
	nod * p=H[x%mod];
	nod * c;
	nod * a;
	c=p;
	
	while(c->val!=x) c=c->urm;
		
	if (c==p){
		a=p;
		p=p->urm;
		delete a;
	}
	else{
		a=c;
		c=c->urm;
		delete a;
	}
}


//Hash::~Hash(){}	
	
int main(){
	
	freopen(file_in,"r",stdin);
	freopen(file_out,"w",stdout);
	
	scanf("%d", &Q);
	
	while(Q--){
		
		scanf("%d %d", &Tip, &x);
		
		if (Tip==1){
			if (!h.cauta(x))
			    h.adauga(x);
		}
		else
		if (Tip==2){
			if (h.cauta(x))
				h.sterge(x);
		}
		else{
			printf("%d\n", h.cauta(x));
		}
	}
	
	
	
	return 0;
}