Cod sursa(job #739577)

Utilizator bogdanrnRadu Bogdan Nicolae bogdanrn Data 23 aprilie 2012 15:33:26
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.47 kb
#include <fstream>
using namespace std;
template <class T>
class nod_e
{
public:
T info;
nod_e *next;
};

template <class T>
class hash_table
{
nod_e<T> *q;
int marime_hash;

public:
hash_table<T>();
void adauga(T);
void sterge(T);
bool gol();
bool cauta(T);
int marime();
nod_e<T>* prim();
nod_e<T>* ultim();
};


template <class T>
nod_e<T>* hash_table<T>::prim()
{
return q;
}

template <class T>
nod_e<T>* hash_table<T>::ultim()
{
nod_e<T> *p;
p=q;
while (p->next != NULL)
p = p->next;
return p;
}
template <class T>
hash_table<T>::hash_table()
{
q = NULL;
marime_hash = 0;
}



template <class T>
bool hash_table<T>::gol()
{
if (marime_hash){
		return true;
		}
	return false;
}

template <class T>
int hash_table<T>::marime()
{
return marime_hash;
}



template <class T>
void hash_table<T>::adauga(T x)
{
nod_e<T> *p,*newnode;
if (cauta(x))
return;

newnode = new nod_e<T>;
newnode->info = x;
newnode->next = NULL;

if (gol())
{
q = newnode;
}
else
{
p = ultim();
p->next = newnode;
}

marime_hash++;
}
template <class T>
bool hash_table<T>::cauta(T x)
{
nod_e<T> *p;
if (gol())
return false;
p = prim();
while (p != NULL)
{
if (p->info == x)
return true;
p = p->next;
}
return false;
}

template <class T>
void hash_table<T>::sterge(T x)
{
nod_e<T> *p, *u;
if (!cauta(x))
return;

if (marime_hash == 1)
{
q = NULL;
}
else
{
u = prim();
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;
}
}
}

marime_hash--;
}

template <class T>
class hash
{
hash_table<T> *tables;
int P;
public:
hash<T>();
bool adauga(T);
bool sterge(T);
short int cauta(T);

};

template <class T>
hash<T>::hash()
{
P = 666013;
tables = new hash_table<T>[P];
}
template <class T>
bool hash<T>::adauga(T x)
{
int table = x%P;

tables[table].adauga(x);
return true;
}
template <class T>
short int hash<T>::cauta(T x)
{
int table = x%P;

if (tables[table].cauta(x))
return 1;
return 0;
}
template <class T>
bool hash<T>::sterge(T x)
{
int table = x%P;
tables[table].sterge(x);
return true;
}
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.adauga(x);
break;
case 2:
H.sterge(x);
break;
case 3:
out<<H.cauta(x)<<'\n';
break;
}
}

return 0;
}