Cod sursa(job #238907)

Utilizator katakunaCazacu Alexandru katakuna Data 3 ianuarie 2009 17:04:55
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include<stdio.h>
#define maxh 666013

struct nod{int inf;nod *urm;} *h[maxh+2];
int i,n,t,x;

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

void add(nod *&p,int x){
nod *q;
   for(q=p;q!=NULL && p->inf != x ;q=q->urm);

   if(q!=NULL) return ;

   nod *t=new nod;
   t->inf=x;
   t->urm=p;
   p=t;
   
}

void find(nod *&p,int x){
nod *q;
   for(q=p;q!=NULL && p->inf != x ;q=q->urm);

   if(q!=NULL) {
   fprintf(g,"1\n");
   return;}

   fprintf(g,"0\n");

}

void erase(nod *&p,int x){
nod *q,*w;

   for(q=p;q!=NULL && p->inf != x ;q=q->urm);

   if(q == NULL) return;

   if(q == p){
   w=p;
   p=p->urm;
   delete w;
   }

   else{
   w=q;
   q=q->urm;
   delete w;
   }
}

int main(){

FILE *f=fopen("hashuri.in","r");
fscanf(f,"%d",&n);

  for(i=1;i<=n;i++){
  fscanf(f,"%d %d",&t,&x);
    if(t==1) add(h[x%maxh],x);
    if(t==2) erase(h[x%maxh],x);
    if(t==3) find(h[x%maxh],x);
  }
  
fclose(f);
fclose(g);

return 0;
}