Cod sursa(job #1045225)

Utilizator PatrunjelFMIAnita Liviu Patrunjel Data 1 decembrie 2013 01:57:48
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include<fstream>
#include<iostream>
using namespace std;

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

const unsigned BUCKETS=5000000; //max(p<100)


struct nod{
    unsigned inf;
    nod *urm;
};
nod *hashtable[BUCKETS];

void adaug(nod *&v,unsigned n){
    if (!v){
        v=new nod;
        v->inf=n;
        v->urm=0;
    }
    else{
        nod *c=new nod;
        c->urm=v;
        c->inf=n;
        v=c;
    }
}
void sterg(nod *&v,unsigned n){
    nod *p=v,*d;

    while(p && p->inf!=n){
        d=p;
        p=p->urm;
    }
    if(p){
        if(p==v){
            v=v->urm;
            delete p;
        }
        else{
            d->urm=p->urm;
            delete p;
        }
    }
}
void find(nod *v,unsigned n){
    for(;v && v->inf!=n;v=v->urm);
    if(v)   fout<<1<<endl;
    else    fout<<0<<endl;
}

int main(){
    unsigned val,n,k,i;
    short opt;

    fin>>n;
    for(i=0;i<n;++i){
        fin>>opt;
        fin>>val;
        k=val%BUCKETS;
        switch(opt){
            case 1:{
                adaug(hashtable[k],val);
                break;
            }
            case 2:{
                sterg(hashtable[k],val);
                break;
            }
            case 3:{
                find(hashtable[k],val);
                break;
            }
        }
    }
    return 0;
}