Cod sursa(job #969613)

Utilizator adrian79Dobrica Adrian adrian79 Data 4 iulie 2013 19:58:52
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <cstdio>
#define N 666013

struct node
{
  long val;
  node* next;
};


struct hashtable
{
  node* T[N];
};

int find(hashtable H, long x)
{
  long code = x % N;
  node* p = H.T[code];
  while (p != NULL && p->val != x)
    p = p->next;
  if (p == NULL)
    return 0;
  else
    if (p->val == x)
      return 1;
}

void add(hashtable &H, long x)
{
  if (find(H,x) == 1)
    return;
  else
  {
    node* aux = new node;
    aux->val = x;
    aux->next = H.T[x%N];
    H.T[x%N] = aux;
  }
}

void del(hashtable &H, long x)
{
  if (H.T[x%N] == NULL)
    return;
  else
  {
    if (H.T[x%N]->val == x)
    {
      node* aux = H.T[x%N];
      H.T[x%N] = H.T[x%N]->next;
    }
    else
    {
      node* p = H.T[x%N];
      while (p != NULL && p->next->val != x)
        p = p->next;
      if (p == NULL)
        return;
      else
      {
        node* aux = p->next;
        p = p->next->next;
        delete aux;
      }
    }
  }
}

hashtable H;

int main()
{
  long n = 0;
  int op = -1;
  long val = 0;
  freopen("hashuri.in","r",stdin);
  freopen("hashuri.out","w",stdout);
  scanf("%ld",&n);
  for (long i=0;i<n;i++)
  {
    scanf("%d%ld",&op,&val);
    switch(op)
    {
      case 1: add(H,val);break;
      case 2: del(H,val);break;
      case 3: printf("%d\n",find(H,val));break;
      default:break;
    }
  }
  return 0;
}