Cod sursa(job #1408667)

Utilizator enedumitruene dumitru enedumitru Data 30 martie 2015 10:25:23
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include<fstream>
#define M 666013
using namespace std;
ifstream f("hashuri.in"); ofstream g("hashuri.out");
struct nod {int e; nod *urm;};
nod *H[M];
void ins(int x)
{   int r=x%M;
    nod *q=new nod;
    q->e=x; q->urm=H[r]; H[r]=q;
}
int caut(int x)
{   int r=x%M;
    for(nod *q=H[r];q;q=q->urm)
        if(x==q->e) return 1;
    return 0;
}
void sterg(int x)
{   int r=x%M;
    if(H[r]->e == x)
    {   nod *q=H[r];
        H[r]=H[r]->urm;
        delete(q);
    }
    else
        {   nod *q=H[r],*p=H[r]->urm;
            for(;p!=NULL && p->e!=x; q=p, p=p->urm);
            if(p!=NULL)
            {   q->urm = p->urm;
                delete(p);
            }
        }
}
int main()
{   int t,op,x;
    f>>t;
    while(t--)
    {   f>>op>>x;
        switch(op)
        {   case 1 : if(!caut(x)) ins(x); break;
            case 2 : if(caut(x)) sterg(x); break;
            case 3 : g<<caut(x)<<'\n';
        }
    }
    g.close(); return 0;
}