Pagini recente » Cod sursa (job #1543554) | Cod sursa (job #1439349) | Cod sursa (job #2423474) | Cod sursa (job #615915) | Cod sursa (job #1200447)
#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) {
list.info = list.next.info;
list.next = list.next.next;
delete list.next;
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;
}