Cod sursa(job #1706941)

Utilizator Gabiap2015Apostol Gabriel Gabiap2015 Data 23 mai 2016 21:15:58
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 3.48 kb
#include "iostream"
#include "string"
#include "cstdint"
#include "string"
#include "fstream"
using namespace std;

#define temp template<class t_key, class t_info>
#define types t_key, t_info

template<class t_key, class t_info>class element{
public:
	t_key key;
	t_info info;
	element<types>* next;
};

template<class t_key, class t_info>class list{
private:
	element<types>* head = NULL;
	element<types>* search_by_info(t_info);
public:
	void addElement(t_key, t_info);
	void deleteEl(t_key);
	element<types>* search_by_key(t_key);
	void display();
	bool exista(t_key);
};

template<class t_key, class t_info>class Mymap{
private:
	static const int MAX = 937;
	list<types> map_element[MAX];
public:
	int hash(const t_key&);
	void push_back(t_key, t_info);
	t_info operator[](t_key);
	void erase(t_key);
	bool exista(t_key);
};

int main()
{
	ifstream hashuri_in("hashuri.in");
	ofstream hashuri_out("hashuri.out");
	Mymap<int, int> a;
	int n;
	hashuri_in >> n;
	for (int i = 0; i < n; i++)
	{
		int x, y;
		hashuri_in >> x >> y;
		if (x == 1)
		{
			a.push_back(y, NULL);
		}
		if (x == 2)
		{
			a.erase(y);
		}
		if (x == 3)
		{
			if (a.exista(y))
				hashuri_out << 1 << endl;
			else
				hashuri_out << 0 << endl;
		}
	}
}



/*list*/
temp element<types>* list<types>::search_by_key(t_key for_search)
{
	if (head == NULL)
		return head;
	element<types>* cur = head;
	while (cur)
	{
		if (cur->key == for_search)
			return cur;
		cur = cur->next;
	}
	return cur;
}
temp element<types>* list<types>::search_by_info(t_info for_search)
{
	if (head == NULL)
		return head;
	element<types>* cur = head;
	element<types>* prev = head;
	while (cur)
	{
		if (cur->info == for_search)
			return cur;
		prev = cur;
		cur = cur->next;
	}
	return cur;
}
temp void list<types>::addElement(t_key cur_key, t_info cur_info)
{
	if (search_by_key(cur_key))
	{
		search_by_key(cur_key)->info = cur_info;
		return;
	}
	element<types>* newEl = new element<types>;
	newEl->info = cur_info;
	newEl->key  = cur_key;
	newEl->next = NULL;
	if (head == NULL)
	{
		head = newEl;
		return;
	}
	element<types> *cur = head;
	while (cur)
	{
		if (cur->next == NULL)
		{
			cur->next = newEl;
			return;
		}
		cur = cur->next;
	}
}
temp void list<types>::deleteEl(t_key cur_info)
{
	element<types> *ptr = search_by_key(cur_info);
	if (ptr == head)
	{
		head = ptr->next;
		return;
	}
	element<types> *cur = head;
	element<types> *prev = head;
	while (cur)
	{
		if (cur == ptr)
		{
			prev->next = cur->next;
			return;
		}
		prev = cur;
		cur = cur->next;
	}
}
temp void list<types>::display()
{
	element<types> *list = head;
	while (list)
	{
		cout << list->info << ' ';
		list = list->next;
	}
	cout << endl;
}
temp bool list<types>::exista(t_key cur)
{
	if (search_by_key(cur))
		return true;
	else
		return false;
}

/*map*/
temp void Mymap<types>::push_back(t_key cur_key, t_info cur_info)
{
	map_element[hash(cur_key)].addElement(cur_key, cur_info);
}
temp t_info Mymap<types>::operator[](t_key cur)
{
	return map_element[hash(cur)].search_by_key(cur)->info;
}
temp int Mymap<types>::hash(const t_key &key)
{
	int i = *(int*)&key;
	return i % MAX;
}
temp void Mymap<types>::erase(t_key cur)
{
	if (map_element[hash(cur)].exista(cur))
		map_element[hash(cur)].deleteEl(cur);
}
temp bool Mymap<types>::exista(t_key cur)
{
	if (map_element[hash(cur)].exista(cur))
		return true;
	return false;	
}