Cod sursa(job #293536)

Utilizator otilia_sOtilia Stretcu otilia_s Data 1 aprilie 2009 21:39:02
Problema Hashuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <stdio.h>
const long mod=524287;
typedef struct nod {int val; nod* next;} *Lista;
Lista h[mod+20];

void inserare(int x)
{
 int i;
 Lista q;
 i=x%mod;
 q=h[i];
 while (q!=NULL)
  {
   if (q->val==x) return;
   q=q->next;
  }
 q=new nod;
 q->val=x; q->next=h[i];
 h[i]=q;
}

void stergere(int x)
{
 int i;
 Lista q,ant;
 i=x%mod;
 q=h[i]; ant=NULL;
 while (q!=NULL)
  {
   if (q->val==x) 
      {
	if (ant==NULL) { h[i]=h[i]->next; delete q;}
	     else
	      {
	       ant->next=q->next;
	       delete q;
	      }
        return;
      }
   ant=q;
   q=q->next;
  }
}

int cauta(int x)
{
 int i; Lista q;
 i=x%mod;
 q=h[i];
 while (q!=NULL)
  {
   if (q->val==x) return 1;
   q=q->next;
  }
 return 0;
}

int main()
{
 FILE *fin=fopen("hashuri.in","r");
 FILE *fout=fopen("hashuri.out","w");
 int n,x,op;
 fscanf(fin,"%d",&n);
 while (n--)
  {
   fscanf(fin,"%d %d",&op,&x);
   switch (op)
    {
     case 1: {inserare(x); break;}
     case 2: {stergere(x); break;}
     case 3: { fprintf(fout,"%d\n",cauta(x));}
    }
  }
 return 0;
}