Cod sursa(job #611740)

Utilizator balakraz94abcd efgh balakraz94 Data 2 septembrie 2011 23:12:36
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include<cstdio>
#define infile "hashuri.in"
#define outfile "hashuri.out"
#define mod 666013 
using namespace std;

typedef struct nod {
	int inf;
	nod *urm;
} *pnod;

pnod L[mod];


pnod find(int);


void add(int x)
{
	int list = x % mod;
	
	pnod p = new nod;
	p->inf = x;
	p->urm = L[list];
	L[list] = p;
}

void erase(int x)
{
	int list = x%mod;
	
    pnod p = find(x);
	
	if(!p)
		return;
	
	if(p == L[list])
	{
		L[list] = L[list]->urm;
		delete p;
		return;
	}
	
	pnod q = p->urm;
	p->urm = p->urm->urm;
	delete q;
}



pnod find(int x)
{
	int list = x%mod;
	
	pnod p = L[list];
	
	if(!p)
		return NULL;
	
	if(p && p->inf == x)
		return p;
	
	while(p->urm)
	{
		if(p->urm->inf == x)
			return p;
		p = p->urm;
	}
	
	return NULL;
}


int main()
{
	freopen(infile,"r",stdin);
	freopen(outfile,"w",stdout);
	
	int op, x, n;
	
	scanf("%d",&n);
	
	while(n--)
	{
		scanf("%d %d",&op,&x);
		
		switch (op)
		{
		case 1: add(x);                             break;  
	    case 2: erase(x);                           break;
	    case 3: printf("%d\n", (find(x)) ? 1 : 0 ); break;
		}
	}
	
	fclose(stdin);
	fclose(stdout);
	
	return 0;
}