Pagini recente » Cod sursa (job #110598) | Cod sursa (job #1850020) | Diferente pentru implica-te/arhiva-educationala intre reviziile 223 si 97 | Cod sursa (job #1959697) | Cod sursa (job #1200444)
#include <iostream>
#include <cstdio>
#include <fstream>
#define HASH_SIZE 666013
using namespace std;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
struct node {
node* next;
int info;
node() {
next = NULL;
info = 0;
}
};
bool lookup(node hash[], int el) {
node& list = hash[el%HASH_SIZE];
while(list.info) {
if(list.info == el)
return true;
list = *list.next;
}
return false;
}
void add(node hash[], int el) {
if (lookup(hash, el)) {
return;
}
node& list = hash[el%HASH_SIZE];
while (list.info) {
list = *list.next;
}
list.info = el;
list.next = new node;
}
void remove(node hash[], int el) {
if (!lookup(hash, el)) {
return;
}
node& list = hash[el%HASH_SIZE];
while(list.info) {
if (list.info == el) {
node* aux = list.next;
list.info = (*aux).info;
list.next = (*aux).next;
delete aux;
return;
}
list = *list.next;
}
}
node hash_table[666013];
int main (int argc, char const *argv[])
{
int n;
in>>n;
int op, el;
for(int i=0; i<n; ++i) {
in>>op>>el;
if (op == 1)
add(hash_table, el);
if (op == 2)
remove(hash_table, el);
if (op == 3)
out<<lookup(hash_table, el)<<"\n";
}
return 0;
}