Cod sursa(job #1196732)

Utilizator Vladinho97Iordan Vlad Vladinho97 Data 9 iunie 2014 00:21:59
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2 kb
#include <cstdio>
#include <stdlib.h>
#define nmax 666200
#define MOD 666013
using namespace std;
struct nod
{
    int val;
    nod* urm;
};
nod *v[nmax];
void inserare(int x)
{
    int rand = x%MOD;
    nod* np = (nod*) malloc(sizeof(nod));
    np->urm = NULL;
    np->val = x;
    if(v[rand]==NULL)
    {
        v[rand]=np;
        return;
    }
    nod* ptr = v[rand];
    nod* anterior;
    while(ptr!=NULL)
    {
        anterior=ptr;
        if(ptr->val==x)
            return;
        ptr=ptr->urm;
    }
    anterior->urm=np;
}
void stergere(int x)
{
    int rand= x%MOD;
    if(v[rand]!=NULL)
    {
       if(v[rand]->val==x)
       {
           v[rand]=v[rand]->urm;
           return;
       }
       nod* ptr=v[rand]->urm;
       nod* anterior=v[rand];
       while(ptr!=NULL)
       {
           if(ptr->val==x)
           {
               if(ptr->urm==NULL)
               {
                   anterior->urm=NULL;
                   ptr==NULL;
               }
               else
               {
                    anterior->urm = ptr->urm;
                    ptr = NULL;
               }
               return;
           }
           anterior=anterior->urm;
           ptr=ptr->urm;
       }

    }

}
bool cautare(int x)
{
    nod* curent=v[x%MOD];
    while (curent!=NULL)
    {
        if(curent->val==x)
            return true;
        curent=curent->urm;
    }
    return false;
}
int main()
{
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    int n,i,op,x;
    scanf("%d",&n);
    for(i=0;i<1000;i++)
    {
        v[i]= NULL ;
    }
    for(i=1;i<=n;i++)
    {
        scanf("%d%d",&op,&x);
        if(op==1)
        {
            inserare(x);
        }
        if(op==2)
        {
            stergere(x);
        }
        if(op==3)
        {
            if(cautare(x)==true)
                printf("1\n");
            else
                printf("0\n");
        }
    }
}