Cod sursa(job #764682)

Utilizator bratualexBratu Alexandru bratualex Data 5 iulie 2012 22:30:09
Problema Hashuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 2.7 kb
#include <fstream>

using namespace std;
const int M = 1000000;
typedef struct lista
{
    int inf;
    lista *next;
}lista;

lista* hash[M];
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");

int op3 (int);
lista *cauta ( int );
int main()
{
    int i,n,op,x,indx;
    fin>>n;
    for(i=0;i<n;i++)
    {
        fin>>op>>x;
        //fout<<"operatia este "<<op<<"  "<<x<<"\n";
        switch (op)
        {
            case 3:
                //fout<<"intru in 3 pt "<<x<<" si afisez :"<<"\n";
                fout <<op3(x)<<"\n";
            break;
            case 2:
                //fout<<"teoretic sterg "<<x<<"\n";
                lista *q,*aux;
                //q=new(lista);
                //aux=new(lista);
                q=cauta (x);
                if ( q )
                {
                    if (q==hash[x%M])
                        //free (q);
                        q=NULL;

                    /*
                    if ( !q->next )
                    {
                        q->inf=NULL;
                    }    //free(q);
                    */
                    else
                    {
                        aux=q->next;
                        q->next=aux->next;
                        aux->inf=NULL;
                        aux->next=NULL;
                    }
                }
            break;
            case 1:
                //fout<<"teoretic adaug "<<x<<"\n";
                if (!op3(x))
                {
                    lista *aux1;
                    indx=x%M;
                    aux1=new(lista);
                    aux1->next=NULL;
                    aux1->inf=x;
                    if (!hash[indx])
                        hash[indx]=aux1;
                    else
                    {
                        q=hash[indx];
                            while ( q->next )
                            {
                                q=q->next;
                            }
                            q->next=aux1;
                    }
                }
            break;
            //default:
            //break;
        }
    }
    fin.close();
    fout.close();
    return 0;
}

int op3 (int x)
{
    int index;
    lista *p;
    //p=new(lista);
    index=x%M;
    p=hash[index];
    while (p!=NULL)
    {
        if (p->inf==x)
            return 1;
        p=p->next;
    }
    return 0;
}

lista *cauta (int x)
{
    int index;
    lista *p,*r;
    //p=new(lista);
    //r=new(lista);
    index=x%M;
    p=hash[index];
    r=p;
    while (p!=NULL)
    {
        if (p->inf==x)
            return r;
        r=p;
        p=p->next;
    }
    return 0;
}