Cod sursa(job #1519562)

Utilizator theprdvtheprdv theprdv Data 7 noiembrie 2015 15:10:06
Problema Hashuri Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 1.6 kb
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define MOD 666013
#define LIST(x) ( x % MOD )
#define foreach(G) if(G) for ( struct hash_line* it = (G) ; it; it = it->next)

struct hash_line{
    int key;
    struct hash_line* next;
} *Hash[MOD + 1];

int key;

char find_val(){
    int list = LIST(key);

    foreach(Hash[list])
        if (it->key == key) return 1;
    return 0;
}

void insert_val(){
    int list = LIST(key);

    foreach(Hash[list]) if (it->key == key) return;

    struct hash_line* New = (struct hash_line*) malloc(sizeof(struct hash_line));
    New->key = key, New->next = Hash[list];
    Hash[list] = New;
}

void erase_val(){
    int list = LIST(key);

    foreach(Hash[list]){
        if (it->key == key){
            struct hash_line *next = Hash[list]->next;
            free(Hash[list]);
            Hash[list] = next;
            return;
        }
        if (!it->next) return;
        if (it->next->key == key){
            struct hash_line *next = it->next->next;
            free(it->next);
            it->next = it->next->next;
            return;
        }
    }
}

int main(){
    int N, type;

    assert(freopen("hashuri.in", "r", stdin));
    freopen("hashuri.out", "w", stdout);

    for (scanf("%d", &N); N; --N){
        scanf("%d %d", &type, &key);
        switch (type){
        case 1:
            insert_val();
            break;
        case 2:
            erase_val();
            break;
        case 3:
            printf("%d\n", find_val());
            break;
        }
    }

    return 0;
}