Cod sursa(job #1611600)

Utilizator h_istvanHevele Istvan h_istvan Data 24 februarie 2016 11:52:11
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#define MOD 2000001

using namespace std;

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

class Hashmap {
private:
	Elem* hashmap[MOD];
public:
	Hashmap() {
		for(int i = 0; i < MOD; ++i)
			hashmap[i] = 0;
	}
	
	Elem* find(int value) {
		Elem* curr = hashmap[value % MOD];
		while (curr) {
			if (curr->value == value) break;
			curr = curr->next;
		}
		return curr;
	}
	
	void remove(int value) {
		Elem* curr = hashmap[value % MOD];
		Elem* prev = 0;
		while (curr) {
			if (curr->value == value) break;
			prev = curr;
			curr = curr->next;
		}
		if (curr) {
			if (prev) prev->next = curr->next;
			else hashmap[value % MOD] = curr->next;
			delete curr;
		}
	}
	
	void insert(int value) {
		Elem* curr = hashmap[value % MOD];
		Elem* newValue = new Elem;
		newValue->value = value;
		hashmap[value % MOD] = newValue;
		if (curr) newValue->next = curr;
		else newValue->next = 0;
	}
};


int main() {
	ifstream input;
	ofstream output;
	input.open("hashuri.in");
	output.open("hashuri.out");
	int n;
	Hashmap *map = new Hashmap();
	input >> n;
	for(int i=0; i < n; ++i) {
		int op, x;
		input >> op >> x;
		if (op == 1) {
			map->insert(x);
		} else if (op == 2) {
			map->remove(x);
		} else {
			Elem* el = map->find(x);
			if (el) output << 1;
			else output << 0;
			output << endl;
		}
	}
	input.close(); 
	output.close();
}