Cod sursa(job #755045)

Utilizator zseeZabolai Zsolt zsee Data 4 iunie 2012 15:40:43
Problema Hashuri Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 1.63 kb
#define m 134999   // nr prim
#define MAXNR 2000000005

#include <stdio.h>
#include <stdlib.h>

/*
** LISTE INLANTUITE
*/

typedef struct LI {
       struct LI *urm;
       unsigned long x;
} li;

void LI_ad( li *c, unsigned long x ) {
     li *n;
     n = (li *)malloc( sizeof( li ) );
     n->urm = c->urm;
     n->x = x;
     c->urm = n;
}

li *LI_srch( li *c, unsigned long x ) {
     li *p;
     p = c->urm;
     while (p != NULL) {
           if ( p->x == x ) {
              return c;
           }
           c = p;
           p = p->urm;
     }
     return NULL;
}

void LI_del( li *c, unsigned long x ) {
     li *p;
     p = LI_srch( c, x );
     if ( p != NULL ) {
          c = p->urm;
          p->urm = p->urm->urm;
          free(c);
     }
}

int hash_simplu( unsigned int z ) {
  return z % m;
}

li *ht;
 
int main(void) {
    
    ht = (li *)calloc( m,sizeof(li) );
    
    FILE *fi,*fo;
    fi = fopen("hashuri.in","r");
    fo = fopen("hashuri.out","w");
    int op, no,i,idx;
    unsigned long x;
    fscanf(fi, "%d", &no);
    
    for (i=0;i<no;i++) {
        fscanf(fi, "%d %d", &op, &x );
        idx = hash_simplu( x );
        switch (op) {
               case 1:
                    if ( LI_srch( &ht[idx], x ) == NULL )
                       LI_ad( &ht[idx], x );
                    break;
               case 2:
                    LI_del( &ht[idx], x );
                    break;
               case 3:
                    fprintf(fo, "%d\n", (LI_srch( &ht[idx], x ) == NULL) ? 0 : 1 );
        }
    }
    fclose(fi);
    fclose(fo);
    return 0;
}