Cod sursa(job #1458286)

Utilizator petru.cehanCehan Petru petru.cehan Data 7 iulie 2015 12:00:15
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
#include <iostream>
#include <fstream>

using namespace std;

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

#define P 1299827

struct nod
{
    int info ;
    nod * next ;
} ;

nod * L [P] ;

void InsertHash ( int x )
{
    for ( nod * p = L [ x % P] ; p != 0 ; p = p->next )
      if ( p->info == x )
          return ;

    nod * p = new nod ;
    p->info = x ;
    p->next = L [ x % P ] ;
    L [ x % P ] = p ;

}

void DeleteHash ( int x )
{
    nod * pred = 0 ;

    for ( nod * p = L [ x % P] ; p != 0 ; p = p->next , pred = p  )
          {
            if ( p->info == x )
              {

                  if ( pred == 0 )  // L-am gasit pe prima pozitie in lista
                    {
                       L [ x % P ] = p->next ; // mut capul listei
                       delete p ;
                       return ;
                    }

                  else
                    {
                       pred->next = p->next ;
                       delete p ;
                       return ;
                    }

              }

          }
}

int IsInSet ( int x )
{
   for ( nod * p = L [ x % P] ; p != 0 ; p = p->next )
        if ( p->info == x )
              return 1 ;
   return 0 ;
}


int N ;

void Citire ()
{
    fin >> N ;
    int a , b ;
    while ( N >= 1 )
    {
        fin >> a >> b ;
        switch ( a )
        {
            case 1 : InsertHash (b) ; break ;
            case 2 : DeleteHash (b) ; break ;
            case 3 : fout << IsInSet (b) << "\n" ; break ;
        }
        -- N ;
    }
}

int main()
{
    Citire () ;
    return 0;

}