Cod sursa(job #898739)

Utilizator tudy23Coder Coder tudy23 Data 28 februarie 2013 11:29:52
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include<cstdio>
using namespace std;
int nr,op,n;
const int cheie=9973;
struct list
{
    int val;
    list *d;
};
list *hash[10000];
int h(int x)
{
    return x%cheie;
}
void sterg(int poz, int x)
{
    list *aux;
    aux=new list;
    aux=hash[poz];
    if(aux->val==x)
    {
        hash[poz]=hash[poz]->d;
        delete aux;
    }
    else{
    while(aux!=0&&aux->d->val!=x)
        aux=aux->d;
    list *cpy;
    cpy=aux->d;
    aux->d=cpy->d;
    delete cpy;}
}
int verif(int poz, int x)
{
    list *aux;
    aux=new list;
    aux=hash[poz];
    while(aux!=0&&aux->d!=0&&aux->d->val!=x)
    {
        aux=aux->d;
    }
    if(aux!=0&&aux->d!=0&&aux->d->val==x)
        return 1;
    if(aux!=0&&aux->val==x)
        return 1;
    return 0;
}
void adaug(int poz, int x)
{
    list *aux;
    aux=new list;
    aux->d=hash[poz];
    aux->val=x;
    hash[poz]=aux;
}
void citire()
{
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    scanf("%d", &n);
    for(int i=0;i<10000;++i)
        hash[i]=0;
    for(int i=1;i<=n;++i)
    {
        scanf("%d%d", &op, &nr);
        if(op==1)
            if(!verif(h(nr),nr))
                adaug(h(nr),nr);
        if(op==3)
            printf("%d\n",verif(h(nr),nr));
        if(op==2)
            if(verif(h(nr),nr))
                sterg(h(nr),nr);
    }

}
int main()
{
    citire();
    return 0;
}