Cod sursa(job #1562991)

Utilizator RodoetTeodor Darie Rodoet Data 5 ianuarie 2016 17:04:41
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <fstream> 
using namespace std;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
const int M = 3999971;
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()
{
    int n, op, key;
    in>>n;
    for (int i = 0; i < n; i++) {
        in>>op>>key;
        switch (op) {
            case 1: addKey(key); break;
            case 2: removeKey(key); break;
            default: out<<get(key) != NULL;
        }
    }
    return 0;
}