Cod sursa(job #472451)

Utilizator robigiirimias robert robigi Data 24 iulie 2010 21:42:28
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
// Hashuri2.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include "stdio.h"

FILE *f=fopen("hashuri.in", "r");
FILE *g=fopen("hashuri.out", "w");


int n;

typedef struct nod
{
	int x;
	struct nod *adr;
};


nod *l[919393];


int cauta(int x)
{
	int cv=x%919393;
	if (l[cv]==NULL) return 0;
	if (l[cv]->x==x) return 1;
	nod *p=new nod;
	p=l[cv];
	while (p->adr!=NULL)
	{
		if (p->adr->x==x) return 1;
	}
	return 0;
}


void inserare(int x)
{
	int cv=x%919393;
	nod *p=new nod;
	p->x=x;
	p->adr=l[cv];
	l[cv]=p;
}


void stergere(int x)
{
	int cv=x%919393;
	if (l[cv]==NULL) return;
	nod *h=new nod;
	if (l[cv]->x==x) 
	{
		h=l[cv]->adr;
		delete l[cv];
		l[cv]=h;
		return ;
	}
	nod *p=new nod;
	p=l[cv];
	while (p->adr!=NULL)
	{
		if (p->adr->x==x)
		{
			h=p->adr->adr;
			delete p->adr;
			p->adr=h;
			return;
		}
	}
	return ;
}



int main()
{
	fscanf(f, "%d", &n);
	for (int i=1; i<=n; i++)
	{
		int o, x;
		fscanf(f, "%d%d", &o, &x);
		if (o==1)
		{
			if (cauta(x)==0)
				inserare(x);
		}
		else
			if (o==2)
			{
				stergere(x);
			}
			else
			{
				fprintf(g, "%d\n", cauta(x));
			}
	}
	return 0;
}