Cod sursa(job #806018)

Utilizator SovStoStoicescu Mihail Cristian SovSto Data 1 noiembrie 2012 18:07:22
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <cstdio>
#define MOD 66013


struct nod
	{
		int val;
		nod *urm;
	} * a[MOD];

void add(int x)
{
	int y=x%MOD;	
	if(a[y]==NULL)
	{
		a[y]=new nod;
		a[y]->val=x;
		a[y]->urm=NULL;
		return;
	}
	nod *p=a[y];
	while(p->urm)
	{
		if(p->val==x)return;
		p=p->urm;
	}
	if(p->val==x)return;
	p->urm=new nod;
	p->urm->val=x;
	p->urm->urm=NULL;
}	

int caut(int x)
{
	int y=x%MOD;
	if(a[y]==NULL)return 0;
	if(a[y]->val==x)return 1;
	nod *p=a[y];
	while(p->urm)
	{
		p=p->urm;
		if(p->val==x)return 1;
	}
	return 0;
}

 void del(int x)
{
	int y=x%MOD;
	if(a[y]==NULL)return;
	if(a[y]->val==x)	{a[y]=NULL;return;}
	nod *p=a[y];
	nod *q=p->urm;
	while(q->val!=x && p->urm!=NULL)
	{
		p=p->urm;
		q=p->urm;
	}
	if(q!=NULL && q->val==x)
		{	if(q->urm==NULL)delete q;
				else {
					p->urm=q->urm;
					delete q;
					}
		}
}

		
				
			
	
	
	

int main()
{
	freopen("hash.in","r",stdin);
	freopen("hash.out","w",stdout);
	int n,c,x;
	scanf("%d",&n);
	while(n--)
	{
		scanf("%d %d",&c,&x);
		if(c==1)add(x);
			else if(c==2) del(x);
				else
					{
						printf("%d",caut(x));
						printf("\n");
					}
	}
}