Pagini recente » Cod sursa (job #2487210) | Cod sursa (job #2243954) | Cod sursa (job #418372) | Cod sursa (job #413208) | Cod sursa (job #2526310)
#include <stdio.h>
#include <stdlib.h>
#define MOD 666013
#define hash(x) (MOD * ((double)(0.618034 * x) - (int)(0.618034 * x)))
#define foreach(G) \
if (G) \
for (hash_line *it = (G); it; it = it->next)
struct hash_line {
int key;
hash_line *next;
} *hash_table[MOD + 1];
int find_val(int key) {
int list = hash(key);
foreach (hash_table[list])
if (it->key == key)
return 1;
return 0;
}
void insert_val(int key) {
int list = hash(key);
foreach (hash_table[list])
if (it->key == key)
return;
hash_line *new = malloc(sizeof(struct hash_line));
new->key = key, new->next = hash_table[list];
hash_table[list] = new;
}
inline void erase_val(int key) {
int list = hash(key);
foreach (hash_table[list]) {
if (it->key == key) {
hash_line *next = hash_table[list]->next;
free(hash_table[list]);
hash_table[list] = next;
return;
}
if (!it->next)
return;
if (it->next->key == key) {
hash_line *next = it->next->next;
free(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(key);
break;
case 2:
erase_val(key);
break;
case 3:
printf("%d\n", find_val(key));
break;
}
}
return 0;
}