Pagini recente » Cod sursa (job #3340146) | Cod sursa (job #3338296) | Cod sursa (job #286371) | Cod sursa (job #287623) | Cod sursa (job #293360)
Cod sursa(job #293360)
#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;
}