Pagini recente » Cod sursa (job #2739771) | Cod sursa (job #1313489) | Cod sursa (job #3235138) | Cod sursa (job #71011) | Cod sursa (job #239723)
Cod sursa(job #239723)
#include<fstream>
using namespace std;
const int M = 9973;
typedef struct List* Lista;
struct List
{
long long Key;
Lista Next;
};
typedef Lista Hash[M];
Hash H;
int i,N,k;
long long x;
ifstream f ("hashuri.in");
ofstream g ("hashuri.out");
void initH(Hash H)
{
for(i=0;i<M;H[i++]=NULL);
}
int hash(long long p)
{
return p%9973;
}
void addH(Hash H, long long P)
{
Lista L=new List;
L->Key=P;
L->Next=H[hash(P)];
H[hash(P)]=L;
}
int searchH(Hash H, long long P)
{
Lista L;
for(L=H[hash(P)];L&&L->Key!=P;L=L->Next);
return L!=NULL;
}
void deleteH(Hash H, long long P)
{
if(H[hash(P)])
{
Lista L=H[hash(P)];
if(L->Key==P)
{
H[hash(P)]=H[hash(P)]->Next;
delete L;
L=NULL;
}
else
{
for(L;L->Next&&L->Next->Key!=P;L=L->Next);
if(L)
{
Lista C=L->Next;
L->Next=C->Next;
delete C;
C=NULL;
}
}
}
}
int main()
{
initH(H);
f>>N;
for(i=0;i<N;i++)
{
f>>k>>x;
if(k==1) addH(H,x);
else if(k==2) deleteH(H,x);
else g<<searchH(H,x)<<"\n";
}
f.close();
g.close();
return 0;
}