Cod sursa(job #2405077)

Utilizator Costy_Suruniuc Constantin Costy_ Data 13 aprilie 2019 21:35:59
Problema Hashuri Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.77 kb
#include <iostream>
#include <fstream>

#define M 666013

using namespace std;

struct node
{
	bool ex = 0;
	int number;
	node *next;
};

node *table[M];

void add(int val)
{
	int temp = val % M;
	node* nod = table[temp];
	if (nod == nullptr)
	{
	}
}

int proc(int op, int val)
{
	int temp = val % M;
	node* &start = table[temp];
	if (start == nullptr)
	{
		if (op == 1)
		{
			start = new node;
			start->ex = 1;
			start->number = val;
			start->next = nullptr;
		}
		if (op == 3)
		{
			return 0;
		}
	}
	else
	{
		if (op == 1)
		{
			bool found = false;
			for (; start != nullptr; start = start->next)
			{
				if (start->number == val)
				{
					found = true;
					break;
				}
			}
			if (!found)
			{
				start = new node;
				start->ex = 1;
				start->number = val;
				start->next = nullptr;
			}
			
		}
		if (op == 2)
		{
			bool found = false;
			if (start->number == val)
			{
				delete start;
				start = nullptr;
				return -1;
			}
			for (; start->next != nullptr; start = start->next)
			{
				if (start->next->number == val)
				{
					found = true;
					cout << start->next->number;
					break;
				}
			}
			if (found)
			{
					node* temp = start->next;
					start->next = temp->next;
					delete temp;
			}
		}
		if (op == 3)
		{
			for (; start != nullptr; start = start->next)
			{
				if (start->number == val)
				{
					return 1;
				}
			}
			return 0;
		}
	}
	return -1;
}


int main()
{
	ifstream in;
	in.open("hashuri.in");
	ofstream out;
	out.open("hashuri.out");
	int n, op, val, res;
	in >> n;
	for (int i = 0; i < n; ++i)
	{
		in >> op >> val;
		res = proc(op, val);
		if (res != -1)
		{
			out << res << '\n';
		}
	}
	in.close();
	out.close();
	
}