Cod sursa(job #2407123)

Utilizator Costy_Suruniuc Constantin Costy_ Data 16 aprilie 2019 15:33:59
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.63 kb
#include <iostream>
#include <fstream>

#define M 666013

using namespace std;

struct node
{
	int number;
	node *next;
};

node *table[M];

void add(int val)
{
	int temp = val % M;
	node*& nod = table[temp];
	if (nod == nullptr)
	{
		nod = new node;
		nod->number = val;
		nod->next = nullptr;
	}
	else
	{
		node* it;
		for (it = nod; it->next != nullptr; it = it->next)
		{
			if (it->number == val)
				return;
		}
		it->next = new node;
		it->next->number = val;
		it->next->next = nullptr;
	}
}
void del(int val)
{
	int temp = val % M;
	node*& nod = table[temp];
	if (nod == nullptr)
	{
		return;
	}
	else
	{
		if (nod->number == val)
		{
			delete nod;
			nod = nullptr;
			return;
		}
		for (node* it = nod; it->next != nullptr; it = it->next)
		{
			if (it->next->number == val)
			{
				node* tnode = it->next;
				it->next = tnode->next;
				delete tnode;
				return;
			}
		}
	}
}

bool ext(int val)
{
	int temp = val % M;
	node*& nod = table[temp];
	if (nod == nullptr)
	{
		return false;
	}
	else
	{
		if (nod->number == val)
		{
			return true;
		}
		node *it;
		for (it = nod; it->next != nullptr; it = it->next)
		{
			if (it->number == val)
			{
				return true;
			}
		}
	}
	return false;
}

int main()
{
	ifstream in;
	in.open("hashuri.in");
	ofstream out;
	out.open("hashuri.out");
	int n, op, val;
	in >> n;
	for (int i = 0; i < n; ++i)
	{
		in >> op >> val;
		switch (op)
		{
		case 1:
			add(val);
			break;
		case 2:
			del(val);
			break;
		case 3:
			out << ext(val) << '\n';
			break;
		}
	}

	in.close();
	out.close();
}