Cod sursa(job #1200440)

Utilizator vlad.doruIon Vlad-Doru vlad.doru Data 22 iunie 2014 14:57:16
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <iostream>
#include <cstdio>
#include <fstream>
#define HASH_SIZE 666013

using namespace std;

ifstream in("hashuri.in");
ofstream out("hashuri.out");

struct node {
  node* next;
  int info;

  // node() {
  //   this.next = NULL;
  //   this.info = 0;
  // }
};

bool lookup(node hash[], int el) {
  node& list = hash[el%HASH_SIZE];

  while(list.info) {
    if(list.info == el)
      return true;
    list = *list.next;
  }
  return false;
}

void add(node hash[], int  el) {
  if (lookup(hash, el)) {
    return;
  }

  node& list = hash[el%HASH_SIZE];
  while (list.info) {
    list = *list.next;
  }
  list.info = el;
  list.next = new node;
}

void remove(node hash[], int el) {
  if (!lookup(hash, el)) {
    return;
  }

  node& list = hash[el%HASH_SIZE];
  while(list.info) {
    if (list.info == el) {
      node* aux = list.next;
      list.info = (*aux).info;
      list.next = (*aux).next;
      delete aux;
      return;
    }
    list = *list.next;
  }
}

node hash[666013];

int main (int argc, char const *argv[])
{
  int n;
  in>>n;
  int op, el;

  for(int i=0; i<n; ++i) {
    in>>op>>el;
    if (op == 1)
      add(hash, el);
    if (op == 2)
      remove(hash, el);
    if (op == 3)
      out<<lookup(hash, el)<<"\n";
  }
  return 0;
}