Cod sursa(job #293360)

Utilizator otilia_sOtilia Stretcu otilia_s Data 1 aprilie 2009 17:32:22
Problema Hashuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <stdio.h>
const long mod=1999993;
typedef struct nod {int val; nod* next;} *Lista;
Lista h[mod+3];


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

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

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

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;
}