Cod sursa(job #1796888)

Utilizator RynaquiAxinte Silviu Rynaqui Data 3 noiembrie 2016 21:07:00
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 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);
int 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
		{
			cout << gaseste_x(h, x) << endl;
		}
	}
	return 0;
}

/*
void init()
{
	for (int i = 0; i < M; i++)
		HT[i] = 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 *aux;
	if (HT[h]){
		if (HT[h]->nr == x)
		{
			if (HT[h]->link == 0)
			{
				delete HT[h];
				HT[h] = 0;
			}
			else
			{
				aux = HT[h];
				HT[h] = HT[h]->link;
				delete aux;
			}
		}
		else
		{
			while (HT[h]->link)
			{
				if (HT[h]->link->nr == x)
				{
					aux = HT[h]->link;
					HT[h]->link = aux->link;
					delete aux;
					break;
				}
			}
		}
	}
}

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