Cod sursa(job #1705963)

Utilizator Gabiap2015Apostol Gabriel Gabiap2015 Data 21 mai 2016 10:11:06
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 3.14 kb
// POO proiect III vInfoarena.cpp : Defines the entry point for the console application.
//

#include "iostream"
#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
{
private:
	t_key key;
	t_info info;
	element *next;
public:
	void set_key(t_key);
	void set_info(t_info);
	void set_point(element*);
	t_key get_key();
	t_info get_info();
	element* get_point();
};

template <class t_key, class t_info> class list
{
private:
	element<t_key, t_info>* head = NULL;
public:
	element<t_key, t_info>* search(t_key);
	t_info operator[](t_key val)
	{
		if (search(val))
		{
			return search(val)->get_info();
		}
		if (!search(val))
		{
			cout << "eroare 1: cheia: " << val << " nu a fost gasita" << endl;
			cout << "executa exit!" << endl;
			exit(1);
		}
	}
	void addEl(t_key, t_info);
	void deleteEl(t_key);
	t_info i_search(t_key);
};

ifstream hashuri_in("hashuri.in");
ofstream hashuri_out("hashuri.out");

int main()
{
	list<int, int> a;
	int n;
	hashuri_in >> n;
	for (int i = 1; i <= n; i++)
	{
		int x, y;
		hashuri_in >> x >> y;
		if (x == 1)
		{
			a.addEl(y, y);
		}
		if (x == 2)
		{
			a.deleteEl(y);
		}
		if (x == 3)
		{
			if (a.i_search(y))
				hashuri_out << 1 << endl;
			else
				hashuri_out << 0 << endl;
		}
	}
	return 0;
}

/*element*/
temp void element<types>::set_key(t_key cur)
{
	key = cur;
}
temp void element<types>::set_info(t_info cur)
{
	info = cur;
}
temp void element<types>::set_point(element* cur)
{
	next = cur;
}

temp t_key element<types>::get_key()
{
	return key;
}
temp t_info element<types>::get_info()
{
	return info;
}
temp element<types>* element<types>::get_point()
{
	return next;
}

/*list*/
temp element<types>* list<types>::search(t_key val)
{
	if (head == NULL)
		return head;
	element<types> *cur = head;
	element<types> *prev = head;
	while (cur)
	{
		if (cur->get_key() == val)
			return cur;
		prev = cur;
		cur = cur->get_point();
	}
	return cur;
}
temp void list<types>::addEl(t_key a, t_info b) {
	if (search(a))
	{
		search(a)->set_info(b);
		return;
	}
	element<types> *newEl = new element<types>;
	newEl->set_key(a);
	newEl->set_info(b);
	newEl->set_point(NULL);

	if (head == NULL) {
		head = newEl;
		return;
	}
	element<types> *cur = head;
	while (cur) {
		if (cur->get_point() == NULL) {
			cur->set_point(newEl);
			return;
		}
		cur = cur->get_point();
	}
}
temp void list<types>::deleteEl(t_key val)
{
	element<types> *ptr = search(val);
	if (ptr == NULL)
		cout << "error 2: Nici un element cu cheia val in lista" << endl;
	if (ptr == head)
	{
		head = ptr->get_point();
		return;
	}
	element<types> *cur = head;
	element<types> *prev = head;

	while (cur)
	{
		if (cur == ptr)
		{
			prev->set_point(cur->get_point());
			return;
		}
		prev = cur;
		cur = cur->get_point();
	}
}
temp t_info list<types>::i_search(t_key val)
{
	if (search(val))
	{
		return search(val)->get_info();
	}
	if (search(val) == NULL)
	{
		return NULL;
	}
}