Pagini recente » Cod sursa (job #2775377) | Cod sursa (job #1938489) | Cod sursa (job #1060963) | Cod sursa (job #872877) | Cod sursa (job #667882)
Cod sursa(job #667882)
#include <stdio.h>
#include <stdlib.h>
struct Element
{
int val;
Element *next;
};
const int MODULO=100003;
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;
}