Cod sursa(job #331855)

Utilizator AnDrEwBoYA Andrei AnDrEwBoY Data 15 iulie 2009 14:55:08
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include<stdio.h>

#define MAX_N 798263
#define h(m) m % MAX_N

struct nod 
{
	int val;
	nod* next;
} *nods[MAX_N];

void read(),add(int),del(int);
int exists(int);

int n;

int main()
{
	freopen("hashuri.in","r",stdin);
	freopen("hashuri.out","w",stdout);
	
	read();
	 
	fclose(stdin); fclose(stdout);
	return 0;
}

void read()
{
	int op,x;
	scanf("%d",&n);
	for(int i = 0; i < n; i++)
	{
		scanf("%d%d",&op,&x);
		switch(op)
		{
			 case 1: add(x);                   break;
		     case 2: del(x);                   break;
		     case 3: printf("%d\n",exists(x)); break; 
		}		 
	}
}

int exists(int x)
{
   nod* curr = nods[h(x)];
   while(curr)
   {
	   if(curr->val == x) return 1;
	   curr = curr->next;
   }
   return 0;
}

void del(int x)
{
    nod *curr;
    for(curr = nods[h(x)]; curr; curr = curr->next)
       if(curr->val == x) break;
	curr = nods[h(x)];
	nods[h(x)] = (nods[h(x)] ? nods[h(x)]->next : NULL );
}

void add(int x)
{
	if(!exists(x))
	{
		nod* New = new nod;
		New->val = x;
		New->next = nods[h(x)];
		nods[h(x)] = New;
	}		
}