Pagini recente » Cod sursa (job #27305) | Cod sursa (job #3223327) | Cod sursa (job #2807824) | Cod sursa (job #1687819) | Cod sursa (job #963202)
Cod sursa(job #963202)
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#ifndef __HASHTABLE__H
#define __HASHTABLE__H
using namespace std;
template<typename T> struct elem {
T info;
struct elem<T> *next;
};
template<typename T> class ForwardList {
public:
elem<T> *pfirst, *plast;
ForwardList();
void addLast(T);
void remove(elem<T> *);
void print();
};
template <typename T>
ForwardList<T>::ForwardList()
{
pfirst = plast = NULL;
}
template <typename T>
void ForwardList<T>::addLast(T val)
{
elem<T> *paux;
paux = new elem<T>;
paux->info = val;
paux->next = NULL;
if(pfirst == NULL)
{
pfirst = plast = paux;
return;
}
plast->next = paux;
plast = paux;
}
template <typename T>
void ForwardList<T>::print()
{
elem<T> *paux;
paux = pfirst;
while(paux)
{
std::cout<<paux->info<<" ";
paux = paux->next;
}
std::cout<<"\n";
}
template <typename T>
void ForwardList<T>::remove(elem<T> *paux)
{
elem<T> *before;
before = paux;
if(paux == NULL)
return;
if(paux == pfirst)
{
pfirst = paux->next;
delete paux;
}
else if(paux == plast)
{
plast = before;
plast->next = NULL;
delete paux;
}
else
{
before->next = paux->next;
delete paux;
}
}
template<typename Tkey> struct nod {
Tkey key;
};
template<typename Tkey> class Hashtable {
private:
ForwardList<struct nod<Tkey> > *H;
unsigned int HMAX;
public:
Hashtable(unsigned int h)
{
HMAX = h;
H = new ForwardList< struct nod<Tkey> >[HMAX];
}
~Hashtable()
{
delete[] H;
}
void put(Tkey key)
{
if(!hasKey(key))
{
unsigned int poz;
poz = key % HMAX;
nod<Tkey> x;
x.key = key;
H[poz].addLast(x);
}
}
bool hasKey(Tkey key)
{
unsigned int poz;
poz = key % HMAX;
struct elem<struct nod<Tkey> >* paux;
paux = H[poz].pfirst;
while(paux && paux->info.key != key)
paux = paux->next;
return paux != NULL;
}
void remove(Tkey key)
{
unsigned int poz = key % HMAX;
elem<nod<Tkey> >* paux = H[poz].pfirst;
while(paux && paux->info.key != key)
paux = paux->next;
H[poz].remove(paux);
}
};
int main()
{
unsigned int n;
short int op;
unsigned int x;
freopen("hashuri.in", "r", stdin);
freopen("hashuri.out", "w", stdout);
scanf("%d", &n);
Hashtable<long> hash(n);
for(unsigned int i = 0; i < n; i++)
{
scanf("%hd", &op);
scanf("%d", &x);
if(op == 1)
hash.put(x);
else if(op == 2)
hash.remove(x);
else
printf("%d\n", hash.hasKey(x));
}
}
#endif // __HASHTABLE__H