Cod sursa(job #1741478)

Utilizator adina0822Ciubotaru Adina-Maria adina0822 Data 14 august 2016 00:33:57
Problema Hashuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
using namespace std;
#include<fstream>
#define P 333331



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

 struct hashh {int inf; hashh *urm;};
 hashh *H[P];
 int n;

 void insert(int x)
 {
     hashh *p;
     if(H[x%P]==NULL)
     {
         H[x%P]=new hashh;
         H[x%P]->inf=x;
         H[x%P]->urm=NULL;
     }
     else
     {
         p=H[x%P]->urm;
         while(p==NULL) p=p->urm;
         p=new hashh;
         p->inf=x;
         p->urm=NULL;
     }
 }
 void del(int x)
 {
     hashh *p,*q;
     p=H[x%P];

    if(p!=NULL && p->inf==x)
     {
         H[x%P]=H[x%P]->urm;
         delete p;
     }
     else if (p!=NULL)
     {

         while(p->urm!=NULL && p->urm->inf!=x) p=p->urm;

         if(p->urm->inf==x)
         {
             q=p->urm;
             p->urm=p->urm->urm;
             delete q;
         }

     }

 }

 bool search (int x)
 {
     hashh *p;

     if(H[x%P]==NULL) return 0;

     p=H[x%P];

     while(p->inf!=x && p->urm!=NULL) p=p->urm;

     if(p->inf==x) return 1;
     return 0;
 }

int main()
{
    int o,x,i;
    f>>n;

    for(i=0; i<P; i++) H[i]=NULL;

    while(n--)
    {
        f>>o>>x;

        if(o==1) insert(x);
        else if(o==2) del(x);
        else g<<search(x)<<'\n';

    }





    return 0;
}