Cod sursa(job #2610702)

Utilizator FLORENTIN-GIULIANO.DUMITRUDumitru Florentin Giuliano FLORENTIN-GIULIANO.DUMITRU Data 5 mai 2020 15:02:38
Problema Hashuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.78 kb
#include <fstream>
#include <iostream>
std::ifstream fin("hashuri.in");
std::ofstream fout("hashuri.out");

struct node{
    long data;
    node *next;

    node () : next(NULL){}
    node (long val, node * a) : data(val) , next(a){}
};

class hashtable{
    const int table;
    node **hash;
public:
    hashtable() : table(666015){
        hash = new node * [table];
    }
    void insert(long val);
    void remove(long val);
    bool search(long val) const;
};

void hashtable::insert(long val) {
    int key = val % table;
    node * adaugat = new node(val,hash[key]);
    hash[key] = adaugat;
}

void hashtable::remove(long val) {
    int key = val % table;
    if (hash[key] != NULL){
        if (hash[key]->data == val)
        {
            hash[key] = hash[key]->next;
            return;
        }
        node * prev = hash[key];
        while (prev ->next){
            if (prev->next->data == val){
                prev ->next = prev->next->next;
            } else
                prev = prev->next;
        }
    }
}

bool hashtable::search(long val) const {
    int key = val % table;
    if (hash[key] != NULL){
        node * prev = hash[key];
        while (prev){
            if (prev->data == val){
                return true;
            }
            prev = prev->next;
        }
    }
    return false;
}

int main() {
    int n;
    fin >> n;
    hashtable ver;
    while (n --){
        int op;
        long val;
        fin >> op >> val;
        switch (op) {
            case 1:
                if (!ver.search(val))
                    ver.insert(val);
                break;
            case 2:
                ver.remove(val);
                break;
            case 3:
                fout << ver.search(val) << "\n";
        }
    }
    fin.close();
    fout.close();
    return 0;
}