Cod sursa(job #741325)

Utilizator blk.irineluIrina Ursateanu blk.irinelu Data 25 aprilie 2012 20:39:51
Problema Hashuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 2.18 kb
#include<fstream>
#include <iostream>
#define Mod 666013

using namespace std;

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

int n;

class element
{
    public :
      int info;
      element *urm;
} *prim, *ultim;

class hash
{
    private:
       element *s;
       int size_of_hash;
    public:
       hash()
       {
           s=NULL;
           size_of_hash=0;
       }
       void insereaza(int x);
       void sterge(int x);
       int cauta(int x);
};

int hash::cauta(int x)
{
    bool ok=0;
    element *p;
    p=prim;
    while(p&&!ok)
    {
        if(p->info==x)
        ok=1;
        p=p->urm;
    }
    return ok;
}

void hash::insereaza(int x)
{
    element *p,*q;
    bool ok;
    if(!prim)
    {
        prim=new element;
        prim->info=x;
        prim->urm=NULL;
        ultim=prim;
    }
    else
    {
        ok=0;
        p=prim;
        while(p->urm)
        {
            if(p->info==x) ok=1;
            p=p->urm;
        }
        if(ok==0)
        {
        q=new element;
        q->info=x;
        p->urm=q;
        ultim=q;
        }
    }
}

void hash::sterge(int x)
{
    if( !cauta(x) )
		return;
	element *p;
    p=prim;
    if(prim->info==x)
    {
        prim=p->urm;
        delete p;
        size_of_hash--;
        return;
    }
    else
    while(p->urm)
    {
        if(p->urm->info==x)
        {
            p->urm=p->urm->urm;
            delete p->urm;
            size_of_hash--;
            return;
        }
    }
}


class hash_table
{
    private:
      hash *H;
    public:
      hash_table()
      {
          H=new hash[Mod];
      }
      void ins(int x)
      {
          H[x%Mod].insereaza(x);
      }
      void sterg(int x)
      {
          H[x%Mod].sterge(x);
      }
      bool caut(int x)
      {
          if(H[x%Mod].cauta(x)) return 1;
          return 0;
      }
};

int main()
{
    int i,op,y;
    f>>n;
    hash_table object;
    for(i=1;i<=n;i++)
    {
        f>>op>>y;
        if(op==1) object.ins(y);
        if(op==2) object.sterg(y);
        if(op==3) g<<object.caut(y)<<"\n";
    }

    return 0;
}