Pagini recente » Cod sursa (job #2066780) | Cod sursa (job #1301724)
#include <fstream>
#include <cstring>
#include <iostream>
using namespace std;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
const int size=2000001;
class Hashtable
{
private:
int v[size];
int hash(int key)
{
return (key%size);
}
public:
Hashtable()
{
memset(v,0,sizeof(v));
}
void insert(int key)
{
int h=hash(key);
while(v[h]!=0 && v[h]!=-1 && v[h]!=key)
{
h++;
if(h>=size) h=0;
}
if(v[h]==0 || v[h]==-1) v[h]=key;
}
bool lookup(int key)
{
int h=hash(key);
while(v[h]!=0 && v[h]!=key)
h++;
if(v[h]==key) return 1;
else return 0;
}
void remove(int key)
{
int h=hash(key);
while(v[h]!=0 && v[h]!=key)
h++;
if(v[h]==key) v[h]=-1;
}
}H;
int main()
{
int T,q,x;
f>>T;
while(T--)
{
f>>q>>x;
switch (q)
{
case 1: H.insert(x); break;
case 2: H.remove(x); break;
case 3: g<<H.lookup(x)<<'\n';
}
}
cout<<clock();
}