Pagini recente » Borderou de evaluare (job #3142300) | Cod sursa (job #1075768) | Profil Sorina.Cojocaru | Cod sursa (job #764645)
Cod sursa(job #764645)
#include <fstream>
using namespace std;
const int M = 100000;
typedef struct lista
{
int inf;
lista *next;
}lista;
lista* hash[M];
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
int op3 (int);
lista *cauta ( int );
int main()
{
//cout << "Hello world!" << endl;
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=cauta (x);
if ( q )
{
if ( !q->next )
{
q->inf=NULL;
} //free(q);
else
{
aux=q->next;
q->next=aux->next;
aux->inf=NULL;
aux->next=NULL;
}
}
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;
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;
index=x%M;
p=hash[index];
r=p;
while (p!=NULL)
{
if (p->inf==x)
return r;
r=p;
p=p->next;
}
return 0;
}