Cod sursa(job #561296)

Utilizator drag0shSandulescu Dragos drag0sh Data 19 martie 2011 18:12:10
Problema Hashuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 kb
#include <stdio.h>
#include <stdlib.h>

#define H 999983

typedef struct nod {
  int val;
  nod * leg;
}nod;

typedef nod* set[H];


void init (set h)
{
  for(int i = 0; i< H;h[i++]=NULL);
}

int contain (set h, int val){
  nod *p;
  p = h[val%H];
  while(p != NULL && p->val != val) 
    p = p->leg;
  return p!=NULL;
}

void add (set h, int val)
{
  nod *p;
  int k = val%H;
  if(contain(h,val)) return;
  p = new nod;
  p ->val = val;
  p ->leg = h[k];
  h[k] = p;
}

void del(set h, int val)
{
  nod *p, *q;
  p = h[ val%H ];
  if(p != NULL &&p->val == val){
    q=p;
    p = p->leg;
    free(q);
    return;
  }
  
  while(p != NULL && p->leg->val != val)
    p = p->leg;
  if(p!=NULL){
    q = p->leg;
    p->leg=p->leg->leg;
    free(q);
  }
}

void prelucrare(){
  FILE *f, *g;
  int op, val, n;
  f = fopen("hashuri.in","r");
  g = fopen("hashuri.out","w");
  fscanf(f,"%d",&n);
  set h;
  init (h);
  while(n--){
    fscanf(f,"%d %d",&op,&val);
    switch(op){
    case 1: 
      add(h,val);
      break;
    case 2:
      del(h,val);
      break;
    case(3):
      fprintf(g,"%d\n",contain(h,val));
    } 
  }
  fclose(f);
  fclose(g);
}

int main(){
  prelucrare();
  return 0;
}