Cod sursa(job #764690)

Utilizator bratualexBratu Alexandru bratualex Data 5 iulie 2012 22:50:02
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.81 kb
#include <fstream>
#include <stdlib.h>
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;
    lista *q,*aux,*aux1;
    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";

                q=new(lista);
                aux=new(lista);
                q=cauta (x);
                if ( q )
                {
                    if (q==hash[x%M])
                        //free (q);
                        {
                            hash[x%M]=q->next;
                            //q=NULL;
                            free (q);
                        }
                    /*
                    if ( !q->next )
                    {
                        q->inf=NULL;
                    }    //free(q);
                    */
                    else
                    {
                        aux=q->next;
                        q->next=aux->next;
                        free (aux);
                        //aux->inf=NULL;
                        //aux->next=NULL;
                    }
                }
            break;
            case 1:
                if (!op3(x))
                {

                    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;
}