Pagini recente » Cod sursa (job #2401760) | Cod sursa (job #3000521) | Cod sursa (job #2758249) | Cod sursa (job #1533393) | Cod sursa (job #1519562)
#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;
}