Cod sursa(job #2153313)

Utilizator stoianmihailStoian Mihail stoianmihail Data 6 martie 2018 09:37:01
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.79 kb
#include <bits/stdc++.h>

#define MOD 213161
#define NIL -1

const char* code[2] = {"1", "0"};

struct nod {
  int val;
  nod *urm;
};

class list {
private:
  nod* l;

  /** Cauta valoarea "x" in lista. **/
  int exist(int x) {
    if (l == NULL) {
      return NIL;
    }

    int pos = 0;
    nod* walk = l;

    while (walk != NULL && walk -> val != x) {
      walk = walk -> urm;
      pos++;
    }
    return walk == NULL ? NIL : pos;
  }

public:
  list() {
    l = NULL;
  }

  /** Adauga valoarea "x" in lista. **/
  void insert(int x) {
    if (exist(x) != NIL) {
      return;
    }

    nod* nou = new nod;
    nou -> val = x;
    nou -> urm = l;
    l = nou;
  }

  void find(int x) {
    nod* walk = l;

    while (walk != NULL && walk -> val != x) {
      walk = walk -> urm;
    }
    puts(code[walk == NULL]);
  }

  /** Sterge valoarea "x" din lista. **/
  void erase(int x) {
    int pos = exist(x);
    if (pos == NIL) {
      return;
    }

    if (l -> urm == NULL) {
      l = NULL;
    } else if (pos == 0) {
      l = l -> urm;
    } else {
      nod* prev = l;
      nod* curr = prev -> urm;
      while (curr -> val != x) {
        prev = curr;
        curr = curr -> urm;
      }
      prev -> urm = curr -> urm;
    }
  }
};

int N;
list hash[MOD];

int main(void) {
  int task, x;
  FILE *f = fopen("hashuri.in", "r");
  freopen("hashuri.out", "w", stdout);

  fscanf(f, "%d", &N);
  while (N) {
    fscanf(f, "%d %d", &task, &x);
    if (task == 1) {
      hash[x % MOD].insert(x);
    } else if (task == 2) {
      hash[x % MOD].erase(x);
    } else {
      hash[x % MOD].find(x);
    }
    N--;
  }
  fclose(f);
  fclose(stdout);

  /// Multumim Doamne!
  puts("Doamne ajuta!");
  return 0;
}