Cod sursa(job #667881)

Utilizator rayvianPricope Razvan rayvian Data 23 ianuarie 2012 21:12:11
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <stdio.h>
#include <stdlib.h>


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

const int MODULO=3571;
typedef struct MyMap
{
  Element *ptr;
};

MyMap  maps[MODULO];

void insert(int elem)
{
  //in ce bucket se situeaza
  int i=elem%MODULO;

  MyMap *t=&maps[i];
  Element *temp=t->ptr;

  while(temp)
  {
    if(temp->val==elem)
      return ;
    temp=temp->next;
  }

  temp=new Element;
  temp->next=t->ptr;
  temp->val=elem;
  t->ptr=temp;
}

void dels(int elem)
{
  //in ce bucket se situeaza
  int i=elem%MODULO;
  MyMap *t=&maps[i];
  Element *temp=t->ptr;
  Element *ant=NULL;

  while(temp)
  {
    if(temp->val==elem)
    {
      if(ant)
        ant->next=temp->next;
      else
        t->ptr=temp->next;
    }
    ant=temp;
    temp=temp->next;
  }
}

int find(int elem)
{
  //in ce bucket se situeaza
  int i=elem%MODULO;
  MyMap *t=&maps[i];
  Element *temp=t->ptr;

  while(temp)
  {
    if(temp->val==elem)
      return 1;
    temp=temp->next;
  }
  return 0;
}

int main()
{
  freopen("hashuri.in","r",stdin);
  freopen("hashuri.out","w",stdout);
  int n;

  scanf("%d",&n);
  for(int i=0; i<n; i++)
  {
    int op,nr;
    scanf("%d %d",&op,&nr);
    switch(op)
    {
      case 1:
        insert(nr);
        break;
      case 2:
        dels(nr);
        break;
      case 3:
        printf("%d\n",find(nr));
    }

  }
  return 0;
}