Pagini recente » Cod sursa (job #309847) | Cod sursa (job #1693113) | Cod sursa (job #933807) | Cod sursa (job #3229749) | Cod sursa (job #714263)
Cod sursa(job #714263)
#include <fstream>
const int P = 666013;
using namespace std;
struct element
{
int info;
element *next;
};
class hash_table
{
element *q;
public:
//int size;
hash_table();
//bool empty();
element* first();
element* last();
};
hash_table::hash_table()
{
q = new element;
q->info = 0;
q->next = NULL;
}
/*bool hash_table::empty()
{
if (size)
return false;
return true;
}*/
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();
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;
p = tables[table].last();
newnode = new element;
newnode->info = x;
newnode->next = NULL;
p->next = newnode;
//tables[table].size++;
}
void hash::erase(int x)
{
element *p, *q;
int table = x%P;
if ((p=(*this).find(x)) != NULL)
{
q = p->next;
p->next = p->next->next;
delete q;
//tables[table].size--;
}
}
int main()
{
int n,op,x;
ifstream f("hashuri.in");
ofstream g("hashuri.out");
hash H;
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;
}