Cod sursa(job #561299)

Utilizator drag0shSandulescu Dragos drag0sh Data 19 martie 2011 18:23:31
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 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;
  int x;
  q = NULL;
  p = h[x=val%H];
  while(p){
    if(p->val == val){
      if(q == NULL)
	h[x] = p->leg;
      else
	q->leg = p->leg;
    free(p);
    return;
  }
  
  q=p;
  p=p->leg;
  }
}

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