Pagini recente » Cod sursa (job #1039248) | Cod sursa (job #899242) | Cod sursa (job #1419563) | Cod sursa (job #2696375) | Cod sursa (job #953138)
Cod sursa(job #953138)
#include<fstream>
using namespace std;
const int MAXN=10001;
ifstream fin("hashuri.in");
ofstream fout("hashuri.out");
typedef struct list_node
{
int value;
list_node *next,*prev;
void init()
{
value=0;
next=prev=NULL;
}
}hash_node;
hash_node *h[MAXN];
int n,k;
int hash_func(int x)
{
static const double y=0.6180339887;
static const int m=1318699;
int key=int((m*(x*y-int(x*y))))%MAXN;
return key;
}
void hash_insert(int x)
{
int key=hash_func(x);
hash_node *curr,*aux;
if (h[key]==NULL)
{
h[key]=new hash_node;
h[key]->init();
}
curr=h[key];
while (curr->next!=NULL)
{
curr=curr->next;
}
aux=new hash_node; aux->init();
aux->prev=curr; curr->next=aux;
aux->value=x;
}
void hash_delete(int x)
{
bool inHash=false;
int key=hash_func(x);
hash_node *curr,*aux;
curr=h[key];
if (h[key]!=NULL)
{
while (!inHash && curr!=NULL)
{
if (curr->value==x)
{
inHash=true;
break;
}
curr=curr->next;
}
}
if (!inHash)
return;
if (curr->next!=NULL)
{
aux=curr->next;
aux->prev=curr->prev;
}
else
{
aux=curr->prev;
aux->next=NULL;
}
delete curr;
}
int hash_search(int x)
{
int key=hash_func(x);
hash_node *curr;
if (h[key]==NULL)
return 0;
curr=h[key]->next;
while (curr!=NULL)
{
if (curr->value==x)
return 1;
curr=curr->next;
}
return 0;
}
int main()
{
int op,x;
fin>>n;
for (k=1;k<=n;++k)
{
fin>>op>>x;
switch(op)
{
case 1:
hash_insert(x);
break;
case 2:
hash_delete(x);
break;
case 3:
fout<<hash_search(x)<<'\n';
break;
}
}
fin.close();
fout.close();
return 0;
}