Cod sursa(job #1427161)

Utilizator aciobanusebiCiobanu Sebastian aciobanusebi Data 1 mai 2015 17:12:11
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
#include<iostream>
#include<fstream>
#define PRIME 3079
#define h(x) ((x) % PRIME)
using namespace std;

struct nod
{
	int info;
	nod *urm;
};

struct List
{
	nod* prim;
};
List T[PRIME];

void insert(List T[], int x)
{
	nod *last = T[h(x)].prim;
	T[h(x)].prim = new nod;
	T[h(x)].prim->info = x;
	T[h(x)].prim->urm = last;
}

void erase(List T[], int x)
{
	if (T[h(x)].prim != NULL)
	{
		nod *p = T[h(x)].prim;
		nod *prec = NULL;
		while (p&&x != p->info)
		{
			prec = p;
			p = p->urm;
		}
		if (p == NULL)
			return;
		if (prec == NULL)
		{
			nod *last = T[h(x)].prim->urm;
			delete T[h(x)].prim;
			T[h(x)].prim = last;
		}
		else
		{
			prec->urm = p->urm;
			delete p;
		}
	}
}

bool check(List T[], int x)
{
	nod *p = T[h(x)].prim;
	while (p)
	{
		if (p->info == x)
			return 1;
		p = p->urm;
	}
	return 0;
}

int main()
{
	ifstream f("hashuri.in");
	ofstream g("hashuri.out");
	int op, x, n;
	f >> n;
	for(int i=1;i<=n;i++)
	{
		f >> op;
		f >> x;
		switch (op)
		{
		case 1:
			insert(T, x);
			break;
		case 2:
			erase(T, x);
			break;
		case 3:
			g << check(T, x)<<'\n';
			break;
		}
	}
	f.close();
	g.close();
	
	int i;
	cin >> i;
	return 0;
}