Pagini recente » Cod sursa (job #2626427) | Cod sursa (job #1555386) | Cod sursa (job #720922) | Cod sursa (job #1011751) | Cod sursa (job #710280)
Cod sursa(job #710280)
#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();
hash_table(int x);
element* first();
element* last();
bool empty();
void init(int);
};
hash_table::hash_table()
{
q = NULL;
size = 0;
}
hash_table::hash_table(int x)
{
q = new element;
q->info = x;
q->next = NULL;
size = 1;
}
element* hash_table::first()
{
return q;
}
element* hash_table::last()
{
element *p;
p=q;
while (p->next != NULL)
p=p->next;
return p;
}
bool hash_table::empty()
{
if (size)
return false;
return true;
}
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;
if (tables[table].empty())
return NULL;
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 (tables[table].empty())
{
hash_table ht(x);
tables[table] = ht;
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 (tables[table].empty())
return;
if ((p=(*this).find(x)) != NULL)
{
if (tables[table].first()->next == NULL)
{
p = NULL;
tables[table].size = 0;
return;
}
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 (; n; --n)
{
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;
}