Cod sursa(job #808432)

Utilizator krissu93FMI Tiugan Cristiana Elena krissu93 Data 6 noiembrie 2012 19:33:36
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;

#define mod 666013
struct nod
{ 	long info;
	nod *urm;
} *lista[mod];

ifstream in("hashuri.in");
ofstream out("hashuri.out");
long n;

void insert (long x)
{
	nod * p; nod *q;
	int ok=1;
	int k=x%mod;
	for (p=lista[k];p&&ok;p=p->urm)
		if (p->info==k) 
			{  ok=0;
				break;
			}
	if (ok)
	{
		q= new nod;
		q->info=x;
		q->urm=lista[k];
		lista[k]=q;
	}
}
long find (long x)
{
	long k=x%mod;
	if ( lista[k]==NULL) return 0 ;
	if (lista[k]->info==x) return 1;
	nod *p=lista[k];
	while (p->urm)
	{
		p=p->urm;
		if (p->info==x) return 1;
	}
	return 0;
}
void deletes (long x)
{	nod *p; nod *q;
 long k=x%mod;
 p=lista[k];
 if (p)
 {
	 if (p->info==x) 
	 {	lista[k]=p->urm;
		 delete p;
	 }
	 else
	 {
		 while (p->urm && p->urm->info !=x)
			 p=p->urm;
		 if (p->urm)
		 {
			 q=p->urm;
			 p->urm=q->urm;
			 delete q;
		 }
	 }
 }
}

int main()
{ long c;
long x;
in>>n;
while (n--)
{
	in>>c>>x;
	if (c==1)
	{
		insert(x);
	}
	else
	if (c==2)
	{
		deletes(x);
	}
	else
	if (c==3)
		out<<find(x)<<endl;
}
return 0;
}