Cod sursa(job #267462)

Utilizator peanutzAndrei Homorodean peanutz Data 27 februarie 2009 15:35:19
Problema Hashuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <stdio.h>
#include <stdlib.h>

#define MOD 610107

typedef struct nod
{
	long v;
	nod *urm;
} *pnod;

pnod list[MOD];
long n;

void baga(int poz, int x)
{
	pnod aux = new nod;
	aux -> v = x;
	aux -> urm = list[poz];
	list[poz] = aux;
}

int find(int poz, int x)
{
	pnod it;
	for(it = list[poz]; it != NULL; it = it -> urm)
		if(it -> v == x)
			return 1;
	return 0;
}

void remove(int poz, int x)
{
	pnod it, last;
	if(list[poz] -> v == x)
	{
		it = list[poz] -> urm;
		delete list[poz];
		list[poz] = it;

		return ;
	}
	else
	{
		last = list[poz];
		for(it = list[poz]; it != NULL; it = it -> urm)
		{
			if(it -> v == x)
			{
				last -> urm = it -> urm;
				delete it;
				it=NULL;
			}
			last = it;
		}
	}
}


int main()
{
	long o, x;
	freopen("hashuri.in", "r", stdin);
	freopen("hashuri.out", "w", stdout);

	scanf("%ld", &n);

	while(n--)
	{
		scanf("%ld %ld", &o, &x);
		if(o == 1)
		{
			if(!find(x%MOD, x))
				baga(x%MOD, x);
                }
		else if(o == 2)
			remove(x%MOD, x);
		else
			printf("%d\n", find(x%MOD, x));
	}
	return 0;
}