Pagini recente » Cod sursa (job #1559839) | Cod sursa (job #373705) | Cod sursa (job #2366615) | Cod sursa (job #1795128) | Cod sursa (job #732197)
Cod sursa(job #732197)
#include <iostream>
#include <fstream>
#define M 199993
using namespace std;
struct nod
{
long val;
nod *next;
};
nod* hash[M];
long hash_function(long key)
{
return key%M;
}
bool search(long key)
{
nod* p;
long 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(long key)
{
long 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(long key)
{
nod* p;
long value=hash_function(key);
for(p=hash[value];p!=NULL;p=p->next)
{
if(p->val==key)
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;long key;
for(long 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;
}