Cod sursa(job #710563)

Utilizator horeste12Stoianovici Horatiu Andrei horeste12 Data 9 martie 2012 23:35:16
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#define hValue 1234577
using namespace std;

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

typedef struct nod
{
    int inf;
    nod *urm;
} NOD;

typedef NOD *hash[hValue];
hash H;

int n,tip,x;

void adaug(int x)
{
    int r=x%hValue;
    for(NOD *i=H[r];i;i=i->urm)
        if(i->inf==x)
            return;
    NOD *p=new NOD;
    p->inf=x;
    p->urm=H[r];
    H[r]=p;
}

void elim(int x)
{
    int r=x%hValue;
    NOD *i=H[r];
    if(!i) return;
    if(H[r]->inf==x)
    {
        H[r]=H[r]->urm;
        return;
    }
    NOD *q=i;
    for(i=H[r]->urm;i;i=i->urm)
    {
        if(i->inf==x)
        {
            q->urm=i->urm;
            return;
        }
        q=i;
    }
}

bool exista(int x)
{
    int r=x%hValue;
    for(NOD *i=H[r];i;i=i->urm)
        if(i->inf==x)
            return true;
    return false;
}

int main()
{
    f>>n;
    for(int i=0;i<n;i++)
    {
        f>>tip>>x;
        if(tip==1)
            adaug(x);
        else if(tip==2)
                elim(x);
            else g<<exista(x)<<'\n';
    }
    return 0;
}