Cod sursa(job #2217246)

Utilizator nurof3nCioc Alex-Andrei nurof3n Data 29 iunie 2018 18:27:31
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>

using namespace std;

ifstream in("hashuri.in");
ofstream out("hashuri.out");

struct myhash
{
    int x;
    myhash *next;
};
const int hash_size = 666013;
int N;
myhash *H[hash_size];

inline int h(int x)
{
    return x % hash_size;
}
myhash* find(int x, myhash *H)
{
    if(H == NULL)
        return NULL;
    if(H->x == x)
        return H;
    for(myhash *p = H; p->next != NULL; p = p->next)
        if(p->next->x == x)
            return p;
    return NULL;
}
void add(int x, myhash *&H)
{
    myhash *nou = new myhash;
    nou->x = x;
    nou->next = H;
    H = nou;
}
void insert(int x, myhash *&H)
{
    if(find(x, H) == NULL)
        add(x, H);
}
void erase(int x, myhash *&H)
{
    myhash *p = find(x, H);
    if(p != NULL)
    {
        myhash *aux;
        if(H->x == x)
        {
            aux = H;
            H = H->next;
            delete aux;
        }
        else
        {
            aux = p->next;
            p->next = p->next->next;
            delete aux;
        }
    }
}
int main()
{
    in >> N;
    int op, x;
    for(int i = 1; i <= N; i++)
    {
        in >> op >> x;
        if(op == 1)
            insert(x, H[h(x)]);
        if(op == 2)
            erase(x, H[h(x)]);
        if(op == 3)
            out << ((find(x, H[h(x)]) == NULL) ? 0 : 1) << '\n';
    }
    return 0;
}