Cod sursa(job #1503246)

Utilizator meriniucrMeriniuc Razvan- Dumitru meriniucr Data 15 octombrie 2015 19:33:44
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.25 kb
#include <fstream>
#include <stdlib.h>

struct value {
	int val;
	value *next;
};

value* hash[98318];

value*	find(int a, int b)
{
	value* it;
	value* temp;

	it = hash[b];
	//std::cout << hash[b] << "A\n";
	temp = hash[b];
	while (it && it -> val != a) {
		//std::cout << it -> val << " ";
		it = it -> next;
	}
	hash[b] = temp;
	return it;
}

value*	createEntry(int a, value* b)
{
	value *r;

	r = new(value);
	r -> next = b;
	r -> val = a;
	std::cout << r -> val << " ";
	return r;
}

int	main()
{
	std::ifstream mama("hashuri.in");
	std::ofstream tata("hashuri.out");

	int	n;
	int	a;
	int	op;
	int	b;
	value	*findReturn;
	value	*r;

	mama >> n;
	for (int i = 0; i < n; i += 1)
	{
		mama >>	op >> a;
		b = a % 98317;
		findReturn = find(a, b);
		if (op == 1 && !findReturn)
		{
			r = createEntry(a, hash[b]);
			hash[b] = r;
			std::cout << hash[b] -> val << "\n";
		}
		else if (op == 2 && findReturn)
		{
			if (hash[b] == findReturn)
				hash[b] = findReturn -> next;
			else
			{
				value *s;
				value *temp;
		
				s = hash[b];
				temp = hash[b];
				while (s -> next -> val != a)
					s = s -> next;
				s -> next = s -> next -> next;
				hash[b] = temp;
			}
		}
		else
		{
			if (findReturn)
				tata << 1 << '\n';
			else
				tata << 0 << '\n';
		}
	}
	return 0;
}