Cod sursa(job #1045670)

Utilizator andreipurdilaAndrei Purdila andreipurdila Data 1 decembrie 2013 21:08:12
Problema Hashuri Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.48 kb
#include <iostream>
#include <fstream>

using namespace std;
unsigned poz=299999;
struct nod
{
    int val;
    nod *urm;
}*my_hash[299999];
ifstream f("hashuri.in");
ofstream g("hashuri.out");
void adaugare(nod *&p, unsigned n)
{
    if (p==NULL){
        p=new nod;
        p->val=n;
        p->urm=NULL;
    }
    else{
        nod *q=new nod;
        q->urm=p;
        q->val=n;
        p=q;
    }
}
void stergere(nod *&p, unsigned x)
{
    nod *q,*t=p;
    while (t&&t->val!=x){
        q=t;
        t=t->urm;
    }
    if (t){
        if (t==p){
            p=p->urm;
            delete t;
        }
        else
        {
            q->urm=t->urm;
            delete t;
        }
    }
}
void my_find(nod *prim,unsigned x)
{
    while (prim&&prim->val!=x){
        prim=prim->urm;
    }
    if (prim)
        g<<1<<endl;
    else
        g<<0<<endl;
}
int main()
{
    unsigned n,x,i,pozitie;
    short answer;
    /*ifstream f("hashuri.in");
    ofstream g("hashuri.out");*/
    f>>n;
    for (i=0;i<n;i++)
    {
        f>>answer;
        f>>x;
        pozitie=x%poz;
        if (answer==1){
            adaugare(my_hash[pozitie],x);
        }
        else if (answer==2)
            stergere(my_hash[pozitie],x);
        else {
            /*if (my_find(my_hash[pozitie],x))
                g<<1<<endl;
            else
                g<<0<<endl;*/
                my_find(my_hash[pozitie],x);
        }
    }
    return 0;
}