Cod sursa(job #1797427)

Utilizator RynaquiAxinte Silviu Rynaqui Data 4 noiembrie 2016 13:48:45
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>
using namespace std;

#define M 123457
struct nod
{
	int nr;
	nod *link;
}*HT[M];

void push(int, int);
void delete_x(int, int);
nod* gaseste_x(int, int);
int main()
{
	int n, o, x,h;
	ifstream cin("hashuri.in");
	ofstream cout("hashuri.out");
	cin >> n;
	for (; n; n--)
	{
		cin >> o >> x;
		h = x%M;
		if (o == 1)
		{
			push(h, x);
		}
		else if (o == 2)
		{
			delete_x(h, x);
		}
		else
		{
			if(gaseste_x(h, x)) cout << 1 << endl;
			else cout << 0 << endl;
		}
	}
	return 0;
}

void push(int h, int x)
{
	nod *a = new nod;
	if (gaseste_x(h, x)) return;
	a->nr = x;
	a->link = HT[h];
	HT[h] = a;
}

void delete_x(int h, int x)
{
	nod *p,*aux;
	p = gaseste_x(h, x);
	if (p)
	{
		p->nr = HT[h]->nr;
		aux = HT[h];
		HT[h] = HT[h]->link;
		delete aux;
	}
}

nod* gaseste_x(int h, int x)
{
	nod *p = HT[h];
	while (p)
	{
		if (p->nr == x) return p;
		p = p->link;
	}
	return 0;
}