Cod sursa(job #883841)

Utilizator Impaler_009Mihai Nitu Impaler_009 Data 20 februarie 2013 14:15:25
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
using namespace std;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
struct node
{int nr;
node *next;
}*v[1000];

void add (int x)
{
	bool ok=1;
	int i=x%1000;
	node *d=v[i];
	while (d!=NULL)
	{
		if (d->nr==x) {ok=0; break;}
		d=d->next;
	}
	if (ok==1)
	{
	d=new node;
	d->nr=x;
	d->next=v[i];
	v[i]=d;
	}
}

void erase (int x)
{
    bool ok=0;
	int i=x%1000;
	node *d=v[i];
	if (d!=NULL) {if (d->nr==x) {v[i]=d->next; delete d;}
	else
	{
	while (d->next!=NULL)
	{
		if (d->next->nr==x) {ok=1; break;}
		d=d->next;
	}
	if (ok)
	{
		node *p=d->next;
		d->next=d->next->next;
		delete p;
	}
	}}
}

void find (int x)
{
	bool ok=0;
	int i=x%1000;
	node *d=v[i];
	while (d!=NULL)
	{
		if (d->nr==x) {fout<<1<<"\n";ok=1; break;}
		d=d->next;
	}
	if (!ok) fout<<0<<"\n";
}

int main ()
{
	int i,n,op,x;
	for (i=1;i<=999;i++) v[i]=NULL;
    fin>>n;
    for (i=1;i<=n;i++)
    {
        fin>>op>>x;
        if (op==1) add (x);
        else if (op==2) erase (x);
        else find (x);
    }
    return 0;
}