Pagini recente » Cod sursa (job #120946) | Cod sursa (job #2561668) | Cod sursa (job #2836436) | Cod sursa (job #951085) | Cod sursa (job #710114)
Cod sursa(job #710114)
#include <fstream>
const int P = 666013;
using namespace std;
struct element
{
int info;
element *next;
};
class hash_table
{
element *q;
public:
hash_table();
element* first();
element* last();
};
hash_table::hash_table()
{
q = new element;
q->info = 0;
q->next = NULL;
}
element* hash_table::first()
{
return q;
}
element* hash_table::last()
{
element *p;
p=q;
while (p->next != NULL)
p=p->next;
return p;
}
class hash
{
hash_table *tables;
public:
hash();
element* find(int);
void insert(int);
void erase(int);
};
hash::hash()
{
tables = new hash_table[P];
}
element* hash::find(int x)
{
element *p;
int table = x%P;
p = tables[table].first();
if (p->info == x)
return p;
while (p->next != NULL)
{
if (p->next->info == x)
return p;
p = p->next;
}
return NULL;
}
void hash::insert(int x)
{
element *p,*newnode;
int table = x%P;
if ((*this).find(x) != NULL)
return;
if ((p=tables[table].first())->info == 0)
{
p->info = x;
return;
}
p = tables[table].last();
newnode = new element;
newnode->info = x;
newnode->next = NULL;
p->next = newnode;
}
void hash::erase(int x)
{
element *p, *q;
int table = x%P;
if ((p=(*this).find(x)) != NULL)
{
if (tables[table].first()->next == NULL)
{
p->info = 0;
return;
}
q = p->next;
p->next = p->next->next;
delete q;
}
}
hash H;
int main()
{
int n,op,x;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
f>>n;
for (int i=0; i<n; ++i)
{
f>>op>>x;
if (op==1)
H.insert(x);
if (op==2)
H.erase(x);
if (op==3)
g<<(H.find(x)!=NULL)<<'\n';
}
return 0;
}