Cod sursa(job #1556618)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 25 decembrie 2015 14:58:31
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <cstdio>
#define M 3999971

using namespace std;

struct nod
{
    int val;
    nod *next;
    nod(int v=0) { this->val = v; this->next = NULL; }
};

nod* hashMap[M];
int getHash(int key)
{
    return key%M;
}
void addKey(int key)
{
    nod *x = new nod(key);
    x->next = hashMap[getHash(key)];
    hashMap[getHash(key)] = x;
}
nod* get(int key)
{
    for (nod *crt = hashMap[getHash(key)]; crt != NULL; crt = crt->next)
        if (crt->val == key)
            return crt;
    return NULL;
}
void removeKey(int key)
{
    nod **pls;
    pls = &hashMap[getHash(key)];
    for (nod *crt = hashMap[getHash(key)]; crt != NULL; pls = &(crt->next), crt = crt->next)
        if (crt->val == key) {
            *pls = crt->next;
            delete crt;
        }
}

int main()
{
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    int n, op, key;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &op, &key);
        switch (op) {
            case 1: addKey(key); break;
            case 2: removeKey(key); break;
            default: printf("%d\n", get(key) != NULL);
        }
    }
    return 0;
}