Cod sursa(job #712992)

Utilizator paul24090FMI - Balauru Paul paul24090 Data 13 martie 2012 23:25:48
Problema Hashuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.67 kb
#include <cstdio>
#include <cmath>
#define MOD 666013

using namespace std;

struct nod
{
	int info;
	nod* urm;
};

class HashTable
{
public:
	HashTable();
	~HashTable();


	bool interogare(const int& x);
	void inserare(const int& x);
	void stergere(const int& x);

private:
	nod *lista[MOD],*ultim[MOD];
};

HashTable::HashTable()
{
	for(int i=0;i<MOD;i++)
	{
		lista[i] = NULL;
		ultim[i] = NULL;
	}
}

HashTable::~HashTable()
{
	for(int i=0;i<MOD;i++)
	{
		if(lista[i]!=NULL)
		{
			nod *p,*q;
			p=lista[i];	
			q=p->urm;
			delete p;
			while(q!=NULL)
			{
				p=q;
				q=q->urm;
				delete p;
			}
		}

	}
}

bool HashTable::interogare(const int& x)
{
	int xx = x % MOD;
	nod *p = lista[xx];
	while(p!=NULL)
	{
		if(p->info == x)
			return true;
		p = p->urm;
	}
	return false;
}

void HashTable::inserare(const int& x)
{
	if(interogare(x))
	{
		int xx = x % MOD;
		nod *p = new nod;
		p->info = x;
		if(lista[xx] == NULL)
		{
			lista[xx] = p;
			ultim[xx] = p;
		}
		else
		{
			ultim[xx]->urm = p;
			ultim[xx] = p;
		}
	}
}

void HashTable::stergere(const int& x)
{
	int xx = x % MOD;
	nod *p,*q;
	p = q = lista[xx];
	while(q != NULL)
	{
		q = q->urm;
		if(q != NULL && q->info == x)
		{
			p->urm = q->urm;
			delete q;
			break;
		}
	}
}

int main()
{
	int op,x,n;

	freopen("hashuri.in","r",stdin);
	freopen("hashuri.out","w",stdout);

	scanf("%d",&n);
	
	HashTable hT;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&op);
		scanf("%d",&x);
		if(op==1)
			hT.inserare(x);
		else if(op==2)
			hT.stergere(x);
		else
			printf("%d\n", hT.interogare(x));
	}
	return 0;
}