Cod sursa(job #1018026)

Utilizator Catalina_BrinzaBrinza Catalina Catalina_Brinza Data 28 octombrie 2013 19:59:18
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb


#include <iostream>
#include <fstream>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");

struct nod
{
    int v;
    nod *next;
};
nod *a[3001]; int p=3001;
void h1(int x)
{nod *m=new nod;
    m->v=x;
    m->next=NULL;
    if (a[x%p]==NULL)
    {
        a[x%p]=new nod;
        a[x%p]=m;
    }
    else
    {
        nod *q=new nod;
        q=a[x%p];
        while (m->next!=NULL) q=q->next;
        q->next=m;
        
    }
    return;
}

void h2(int x)
{
    if (a[x%p]!=NULL)
    {
    nod *q=new nod;
    nod *m=new nod;
        if (a[x%p]->v==x){ a[x%p]=NULL;return;}
       else {m=a[x%p];
    q=m->next;
    while (m->next!=NULL)
        if (q->v==x) {
            m=q->next;
            delete q;
            return;
        }
           else {m=q; q=q->next;}}}
    return;
}

void h3(int x)
{if (a[x%p]!=NULL) {
    if (a[x%p]->v==x){g<<1<<"\n"; a[x%p]=NULL; return;}
    else {
    nod *m=new nod;
        m=a[x%p];
    while (m->next!=NULL || m==NULL)
        if (m->v==x){ g<<1<<"\n";return;}
        else m=m->next;
    g<<0<<"\n";return;
    }}
else g<<0<<"\n";
    return;
}



int main()
{int i,n,x,y;
    f>>n;
    for (i=0;i<p;i++) a[i]=NULL;
    for (i=0;i<n;i++)
    {
        f>>y>>x;
        if (y==1) h1(x);
        else if (y==2) h2(x);
        else h3(x);
    }
    f.close();
    g.close();
    return 0;

        
}