Cod sursa(job #519724)

Utilizator szabibibiOrban Szabolcs szabibibi Data 6 ianuarie 2011 13:04:19
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include <stdio.h>
#define P 999983

typedef struct lista
{
	struct lista *next;
	long x;
} lista;

lista *a[P];
lista *k;
long n,i;

long hash(long snake)
{
	return snake % P;
}


int search_in_list(long has, long x)
{
	k = a[has];
	if (k==NULL) return 0;
	while (k->x != x && k->next != NULL)
		k = k->next;
	if (k->x == x)
		return 1;
	else
		return 0;
}



void add_to_list(long has, long x)
{
	if (a[has] == NULL)
	{
		a[has] = new lista;
		a[has]->x = x;
		a[has]->next = NULL;
	}
	else if (search_in_list(has, x))
	{}
	else
	{
		k = new lista;
		k->x = x;
		k->next = a[has];
		a[has] = k;
	}
}


void add_to_set(long x)
{
	add_to_list(hash(x), x);
}


void remove_from_list(long has, long x)
{
	k = a[has];
	if (k->x == x)
	{
		delete k;
		a[has] = NULL;
	}
	else
	{
		while (k->next != NULL && k->next->x != x)
			k = k->next;
		k->next = k->next->next;
		delete k->next;
	}
}



void remove_from_set(long x)
{
	if (search_in_list(hash(x),x))
		remove_from_list(hash(x), x);
}

void is_in_set(long x)
{
	if (search_in_list(hash(x), x))
		printf("1\n");
	else
		printf("0\n");
}





int main()
{
	int op;
	long x;

	freopen("hashuri.in","r",stdin);
	freopen("hashuri.out","w",stdout);

	scanf("%ld", &n);

	for (i = 1; i<=n; i++)
	{
		scanf("%d %ld", &op, &x);
		switch (op)
		{
			case 1:
				add_to_set(x);
				break;
			case 2:
				remove_from_set(x);
				break;
			case 3:
				is_in_set(x);
				break;
		}
	}
	return 0;
}