Pagini recente » Cod sursa (job #463325) | Cod sursa (job #1058573) | Cod sursa (job #308216) | Cod sursa (job #744444) | Cod sursa (job #712992)
Cod sursa(job #712992)
#include <cstdio>
#include <cmath>
#define MOD 666013
using namespace std;
struct nod
{
int info;
nod* urm;
};
class HashTable
{
public:
HashTable();
~HashTable();
bool interogare(const int& x);
void inserare(const int& x);
void stergere(const int& x);
private:
nod *lista[MOD],*ultim[MOD];
};
HashTable::HashTable()
{
for(int i=0;i<MOD;i++)
{
lista[i] = NULL;
ultim[i] = NULL;
}
}
HashTable::~HashTable()
{
for(int i=0;i<MOD;i++)
{
if(lista[i]!=NULL)
{
nod *p,*q;
p=lista[i];
q=p->urm;
delete p;
while(q!=NULL)
{
p=q;
q=q->urm;
delete p;
}
}
}
}
bool HashTable::interogare(const int& x)
{
int xx = x % MOD;
nod *p = lista[xx];
while(p!=NULL)
{
if(p->info == x)
return true;
p = p->urm;
}
return false;
}
void HashTable::inserare(const int& x)
{
if(interogare(x))
{
int xx = x % MOD;
nod *p = new nod;
p->info = x;
if(lista[xx] == NULL)
{
lista[xx] = p;
ultim[xx] = p;
}
else
{
ultim[xx]->urm = p;
ultim[xx] = p;
}
}
}
void HashTable::stergere(const int& x)
{
int xx = x % MOD;
nod *p,*q;
p = q = lista[xx];
while(q != NULL)
{
q = q->urm;
if(q != NULL && q->info == x)
{
p->urm = q->urm;
delete q;
break;
}
}
}
int main()
{
int op,x,n;
freopen("hashuri.in","r",stdin);
freopen("hashuri.out","w",stdout);
scanf("%d",&n);
HashTable hT;
for(int i=1;i<=n;i++)
{
scanf("%d",&op);
scanf("%d",&x);
if(op==1)
hT.inserare(x);
else if(op==2)
hT.stergere(x);
else
printf("%d\n", hT.interogare(x));
}
return 0;
}