Cod sursa(job #1465611)

Utilizator k_ounu_eddyIacob Eduard k_ounu_eddy Data 27 iulie 2015 18:48:03
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.92 kb
#include<iostream>
#include<fstream>
#include<string.h>
#define MOD 666013
using namespace std;

struct nod
{
    int info;
    nod* urm;
};

void add(nod *&start, int _info)
{    
     if(start == NULL)
     {
         start = new nod;
         start->info = _info;
         start->urm = NULL;
     } else 
     {
         nod *aux = start;
         while(aux->urm)
         {
             aux = aux->urm;             
         }

         nod *n = new nod;
         n->info = _info;
         n->urm = NULL;
         aux->urm = n;
     }
}

void del(nod *&start, int _info)
{
    if(start->info == _info)
    {
        nod *n = start;
        start = start->urm;
        delete n;
    } else
    {
        nod *n = start;

        while(n!= NULL)
        {
            if((n->urm!= NULL) && (n->urm->info == _info))
                break;

            n = n->urm;
        }

        if(n!=NULL)
        {
            nod *toDel = n->urm;
            n->urm = n->urm->urm;
            delete toDel;
        }
    }
}


void list(nod *start)
{
    while(start)
    {
        cout<<start->info<<" ";
        start = start->urm;
    }
    cout<<endl;
}

bool find(nod *start, int _info)
{
    nod *n = start;
    bool gasit = false;
    while((n!= NULL) && !gasit)
    {
        if(n->info == _info)
            gasit = true;

        n = n->urm;
    }

    return gasit;
}

int main()
{
    fstream fin, fout;
    fin.open("hashuri.in");
    fout.open("hashuri.out");
    int N;
    fin>>N;

    int a,b;    
    nod *n[MOD+1];
    memset(n,MOD+1, 0);

   
    for(int i=0;i<N;i++)
    {
        fin>>a>>b;
        if(a == 1)
        {
            add(n[b%MOD], b);
        } else if(a==2)
        {
            del(n[b%MOD], b);
        } else if(a==3)
        {
            if(find(n[b%MOD], b) == true)
                fout<<"1"<<endl;
            else
                fout<<"0"<<endl;
        }        
    }
  
    fin.close();
    fout.close();
    return 0;
}