Cod sursa(job #2526314)

Utilizator theprdvtheprdv theprdv Data 18 ianuarie 2020 14:43:00
Problema Hashuri Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.6 kb
#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)

typedef struct hash_line {
  int key;
  struct hash_line *next;
} hash_line;

hash_line *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;
}

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, key;

  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;
}