Cod sursa(job #1464991)

Utilizator TeodorescuStefanEduardTeodorescu Stefan Eduard TeodorescuStefanEduard Data 26 iulie 2015 11:39:17
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <stdlib.h>
#define MOD 666013
#define list (key % MOD)
#define foreach(G) if(G) for ( hash_line* it = (G) ; it; it = it->next)
 
using namespace std;
 
struct hash_line{
    int key;
    hash_line* next;
} *Hash[MOD];
 
int key;
 
inline bool find_val(){
    foreach(Hash[list])
        if (it->key == key) return 1;
    return 0;
}
 
inline void insert_val(){
    foreach(Hash[list]) if (it->key == key) return;
 
    hash_line *New = new hash_line;
    New->key = key, New->next = Hash[list];
    Hash[list] = New;
}
 
inline void erase_val(){
    foreach(Hash[list]){
        if (it->key == key){
            hash_line *next = Hash[list]->next;
            delete Hash[list];
            Hash[list] = next;
            return;
        }
        if (!it->next) return;
        if (it->next->key == key){
            hash_line *next = it->next->next;
            delete it->next;
            it->next = it->next->next;
            return;
        }
    }
}
 
int main(){
    int N, type;
 
    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;
 
}