Pagini recente » Cod sursa (job #2460519) | Cod sursa (job #2047574) | Cod sursa (job #1952597) | Cod sursa (job #2040361) | Cod sursa (job #1949797)
#include<cstdio>
using namespace std;
const int MOD = 666013;
struct lista
{
int val;
lista *next;
};
lista *Hash[MOD];
int Search(int key)
{
lista *l = Hash[key % MOD];
while(l != NULL && l->val != key)
l = l->next;
if(l == NULL)
return 0;
return 1;
}
void Insert(int key)
{
lista *elem = new lista;
elem->val = key;
elem->next = Hash[key % MOD];
Hash[key % MOD] = elem;
}
void Delete(int key)
{
lista *l = Hash[key % MOD];
if(l->val == key)
Hash[key % MOD] = l->next;
else
{
while(l->next->val != key)
l = l->next;
l->next = l->next->next;
}
}
int main()
{
int n, op, x, i;
FILE *fin, *fout;
fin = fopen("hashuri.in","r");
fout = fopen("hashuri.out","w");
fscanf(fin,"%d",&n);
for(i=1; i<=n; i++)
{
fscanf(fin,"%d%d",&op,&x);
if(op == 1)
{
if(!Search(x))
Insert(x);
}
if(op == 2)
{
if(Search(x))
Delete(x);
}
if(op == 3)
{
if(Search(x))
fprintf(fout,"1\n");
else fprintf(fout,"0\n");
}
}
fclose(fin);
fclose(fout);
return 0;
}