Cod sursa(job #2045419)

Utilizator xRoALexBirtoiu Alexandru xRoALex Data 22 octombrie 2017 13:04:51
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <cstdio>
#define P 666013
using namespace std;

FILE *f= fopen("hashuri.in","r");
FILE *g= fopen("hashuri.out","w");

int n;

struct lista
{
    int x;
    lista *next;
};
lista *v[P+1]={0,NULL};

int gasire(int x)
{
    int nr=x%P;
    lista *cap=v[nr];
    while(cap!=NULL)
    {
        if(cap->x==x)
            return 1;
        cap=cap->next;
    }
    return 0;
}

void adaugare(int x)
{
    int  nr= x%P;
    if(gasire(x)==0)
    {
    lista *l= new lista;
    l -> x = x;
    l -> next = v[nr];
    v[nr]=l;
    }
}

void stergere(int x)
{
    int  nr= x%P;

    if (v[nr] == NULL)
        return;

    if (v[nr]->x == x)
    {
        lista* el = v[nr];
        v[nr] = v[nr]->next;
        delete el;
        return;
    }
        lista* elem = v[nr];
            while (elem->next != NULL && elem->next->x != x)
                elem = elem->next;

        if (elem->next != NULL)
            {
                lista* deSters = elem->next;
                elem->next = elem->next->next;
                delete deSters;
            }
}



int main()
{
    fscanf(f,"%d",&n);
    for(int i=1; i<=n; i++)
    {
            int cer,x;
            fscanf(f,"%d%d",&cer,&x);
            if(cer==1)
                adaugare(x);
            else if(cer==2)
                stergere(x);
            else fprintf(g,"%d\n",gasire(x));
    }
    return 0;
}