Pagini recente » Cod sursa (job #2310376) | Cod sursa (job #479247) | Cod sursa (job #989561) | Cod sursa (job #1410209) | Cod sursa (job #734180)
Cod sursa(job #734180)
#include <fstream>
using namespace std;
const int FNV=16777619;
template <class T>
class element
{
public:
T info;
element *next;
};
template <class T>
class hash_table
{
element<T> *q;
int size_of_hash;
public:
hash_table<T>();
void insert(T);
void erase(T);
bool empty();
bool find(T);
int size();
element<T>* first();
element<T>* last();
};
template <class T>
hash_table<T>::hash_table()
{
q = NULL;
size_of_hash = 0;
}
template <class T>
bool hash_table<T>::empty()
{
return size_of_hash ? false : true;
}
template <class T>
int hash_table<T>::size()
{
return size_of_hash;
}
template <class T>
element<T>* hash_table<T>::first()
{
return q;
}
template <class T>
element<T>* hash_table<T>::last()
{
element<T> *p;
p=q;
while (p->next != NULL)
p = p->next;
return p;
}
template <class T>
bool hash_table<T>::find(T x)
{
element<T> *p;
if (empty())
return false;
p = first();
while (p != NULL)
{
if (p->info == x)
return true;
p = p->next;
}
return false;
}
template <class T>
void hash_table<T>::insert(T x)
{
element<T> *p,*newnode;
if (find(x))
return;
newnode = new element<T>;
newnode->info = x;
newnode->next = NULL;
if (empty())
{
q = newnode;
}
else
{
p = last();
p->next = newnode;
}
size_of_hash++;
}
template <class T>
void hash_table<T>::erase(T x)
{
element<T> *p, *u;
if (!find(x))
return;
if (size_of_hash == 1)
{
q = NULL;
}
else
{
u = first();
if (u->info == x)
q = q->next;
else
{
p = u->next;
while (p != NULL)
{
if (p->info == x)
{
u->next = p->next;
delete p;
break;
}
u = p;
p = p->next;
}
}
}
size_of_hash--;
}
template <class T>
class hash
{
hash_table<T> *tables;
int P;
public:
hash<T>();
bool insert(T);
bool erase(T);
short int find(T);
};
template <class T>
hash<T>::hash()
{
P = 666013;
tables = new hash_table<T>[P];
}
template <class T>
bool hash<T>::insert(T x)
{
int table = x%P;
tables[table].insert(x);
return true;
}
template <class T>
bool hash<T>::erase(T x)
{
int table = x%P;
tables[table].erase(x);
return true;
}
template <class T>
short int hash<T>::find(T x)
{
int table = x%P;
if (tables[table].find(x))
return 1;
return 0;
}
int main()
{
int n,op,x;
ifstream in("hashuri.in");
ofstream out("hashuri.out");
hash<int> H;
in>>n;
for (; n; --n)
{
in>>op>>x;
switch (op)
{
case 1:
H.insert(x);
break;
case 2:
H.erase(x);
break;
case 3:
out<<H.find(x)<<'\n';
break;
}
}
return 0;
}