Cod sursa(job #1047716)

Utilizator buzu.tudor67Tudor Buzu buzu.tudor67 Data 4 decembrie 2013 20:33:19
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include<fstream>
#define ro 666013
using namespace std;
ifstream fi("hashuri.in");
ofstream fo("hashuri.out");

struct nod{
       int info;
       nod *next;
       };
       
nod *a[ro],*q,*p;
int t,i,oper,x;

int apartine(int x){
    int k=x%ro;
    
    q=a[k];
    while(q!=NULL && q->info!=x) q=q->next;
    
    if(q!=NULL) return 1;
    else return 0;
}

void adauga(int x){
     int k=x%ro;
     
     q=new nod;
     q->info=x;
     q->next=a[k];
     a[k]=q;
}

void sterge(int x){
     int k=x%ro;
     q=a[k];
     
     if(q->info==x){
                    a[k]=q->next;
                    delete q;
                   }
     else {
           p=q;
           while(q!=NULL && q->info!=x) { p=q; q=q->next; }
           p->next=q->next;
           delete q;
          }
}

int main(){
    fi>>t;
    
    for(i=0;i<ro;i++) a[i]=NULL;
    
    for(i=1;i<=t;i++){
                      fi>>oper>>x;
                      if(oper==1){
                                  if(apartine(x)==0) adauga(x);
                                 }
                      else if(oper==2){
                                       if(apartine(x)) sterge(x);
                                      }
                      else if(oper==3){
                                       if(apartine(x)) fo<<"1\n";
                                       else fo<<"0\n";
                                      }
                     }
    
    fi.close();
    fo.close();
    return 0;
}