Cod sursa(job #561671)

Utilizator desoComan Andrei deso Data 21 martie 2011 01:40:32
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <iostream>
#include <fstream>
using namespace std;

#define INFILE "hashuri.in" 
#define OUTFILE "hashuri.out"

ifstream fin (INFILE);
ofstream fout (OUTFILE);

#define MOD 660053

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

nod* table[MOD];

int contains(int x)
{
  nod* n = table[x % MOD];
  while( n!=NULL)
  {
    if( n->val==x )
      return 1;
    n = n->next;
  }
  return 0;
}

void add(int x)
{
  nod* n = table[x % MOD];
  if( n==NULL )
  {
    nod* nx = new nod();
    nx->val = x;
    nx->next = NULL;
    table[x % MOD] = nx;
  }
  else
  {
    if( n->val==x )
      return;
    while( n->next!=NULL && n->next->val!=x )
      n = n->next;
    if( n->next==NULL )
    {
      nod* nx = new nod();
      nx->val = x;
      nx->next = NULL;
      n->next = nx;
    }
  }
}

void del(int x)
{
  nod* n = table[x % MOD];
  if( n==NULL ) 
    return;
  if( n->val==x )
  {
    table[x % MOD] = n->next;
    delete n;
  }
  else
    while( n->next!=NULL )
    {
      if( n->next->val==x )
      {
        nod* aux = n->next;
        n->next = aux->next;
        delete aux;
      }
      n = n->next;
    }
}

int main()
{
  int n, nr;
  int op;
  memset(table, NULL, sizeof(table));
  fin >> n;
  while( n-- )
  {
    fin >> op >> nr;
    if( op==1 ) add(nr);
    else if( op==2 ) del(nr);
    else fout << contains(nr) << endl;
  }
	
	return 0;
}