Pagini recente » Cod sursa (job #2025921) | Cod sursa (job #2955448) | Cod sursa (job #2958732) | Cod sursa (job #917175) | Cod sursa (job #764688)
Cod sursa(job #764688)
#include <fstream>
#include <stdlib.h>
using namespace std;
const int M = 1000000;
typedef struct lista
{
int inf;
lista *next;
}lista;
lista* hash[M+3];
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
int op3 (int);
lista *cauta ( int );
int main()
{
int i,n,op,x,indx;
fin>>n;
for(i=0;i<n;i++)
{
fin>>op>>x;
//fout<<"operatia este "<<op<<" "<<x<<"\n";
switch (op)
{
case 3:
//fout<<"intru in 3 pt "<<x<<" si afisez :"<<"\n";
fout <<op3(x)<<"\n";
break;
case 2:
//fout<<"teoretic sterg "<<x<<"\n";
lista *q,*aux;
//q=new(lista);
//aux=new(lista);
q=cauta (x);
if ( q )
{
if (q==hash[x%M])
//free (q);
{
hash[x%M]=q->next;
free(q);
}
/*
if ( !q->next )
{
q->inf=NULL;
} //free(q);
*/
else
{
aux=q->next;
q->next=aux->next;
free(q);
}
}
break;
case 1:
//fout<<"teoretic adaug "<<x<<"\n";
if (!op3(x))
{
lista *aux1;
indx=x%M;
aux1=new(lista);
aux1->next=NULL;
aux1->inf=x;
if (!hash[indx])
hash[indx]=aux1;
else
{
q=hash[indx];
while ( q->next )
{
q=q->next;
}
q->next=aux1;
}
}
break;
//default:
//break;
}
}
fin.close();
fout.close();
return 0;
}
int op3 (int x)
{
int index;
lista *p;
//p=new(lista);
index=x%M;
p=hash[index];
while (p!=NULL)
{
if (p->inf==x)
return 1;
p=p->next;
}
return 0;
}
lista *cauta (int x)
{
int index;
lista *p,*r;
//p=new(lista);
//r=new(lista);
index=x%M;
p=hash[index];
r=p;
while (p!=NULL)
{
if (p->inf==x)
return r;
r=p;
p=p->next;
}
return 0;
}