Pagini recente » Cod sursa (job #506635) | Cod sursa (job #36625) | Cod sursa (job #2792032) | Cod sursa (job #156555) | Cod sursa (job #732123)
Cod sursa(job #732123)
#include <iostream>
#include <fstream>
#define M 504991
using namespace std;
struct nod
{
int val;
nod *next;
};
nod* hash[M];
int hash_function(int key)
{
return key%M;
}
int search(int key)
{
nod* p;
int value=hash_function(key);
p=hash[value];
while(p!=NULL && p->val!=key)
p=p->next;
if (p!=NULL)
return 1;
else
return 0;
}
void add(int key)
{
int value=hash_function(key);
for(nod *p=hash[value];p!=NULL;p=p->next)
if(p->val==key)
return;
nod* q;
q=new nod;
q->val=key;
q->next=hash[value];
hash[value]=q;
}
void del(int key)
{
nod* p;
int value=hash_function(key);
for(p=hash[value];p!=NULL;p=p->next)
{
if(p->val==value)
break;
}
if(p==NULL)
return;
p->val=hash[value]->val;
p=hash[value];
hash[value]=hash[value]->next;
delete p;
}
int main()
{
ifstream fin("hashuri.in");
long n;
ofstream fout("hashuri.out");
fin>>n;
int opt;int key;
for(int i=0;i<n;i++)
{
fin>>opt;
fin>>key;
switch (opt)
{
case 1:
{
add(key);
break;
}
case 2:
{
del(key);
break;
}
case 3:
{
fout<<search(key)<<"\n";
break;
}
default:
break;
}
}
fout.close();
fin.close();
return 0;
}