Cod sursa(job #239745)

Utilizator mihai.cuculiciCuculici Mihail mihai.cuculici Data 5 ianuarie 2009 18:25:03
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.52 kb
#include<fstream>
using namespace std;

const int M = 666013;   
typedef struct List* Lista;
struct List
{
   long long Key;    
   Lista Next;
}; 
typedef Lista Hash[M];
Hash H;
int op;
long long x,i,N;

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

int hash(long long p)
{
    return p%M;
}

void addH(Hash H, long long P)
{
     int x=hash(P);
     if(!H[x])
     {
        H[x]=new List;
        H[x]->Key=P;   
        H[x]->Next=NULL;   
     }
     else
     {
         Lista L=H[x];
         for(;L&&L->Key!=P;L=L->Next);
         if(!L)
         {
               L=new List;
               L->Key=P;
               L->Next=H[x];
               H[x]=L;
         }
     }
}

int searchH(Hash H, long long P)
{
       int x=hash(P);      
       Lista L;
       for(L=H[x];L&&L->Key!=P;L=L->Next);
       return L!=NULL;
}

void deleteH(Hash H, long long P)
{
     int x=hash(P);
     Lista L=H[x];
     for(L=H[x];L&&L->Key!=P;L=L->Next);
     if(!L) return;
     if(L->Key==P) 
     {
         H[x]=H[x]->Next;              
         delete L;
     }
     else
     {
         Lista L2 = L;
         L=L->Next;
         delete L2;
     }
}

int main()
{
    f>>N;
    for(i=0;i<N;i++)
    {
        f>>op>>x;
        switch(op)   
        {
             case 1: addH(H, x);    break;
             case 2: deleteH(H, x); break;
             case 3: g<<searchH(H, x)<<"\n"; break;        
        }
    }
    f.close();
    g.close();
    return 0;
}