Cod sursa(job #2591107)

Utilizator bogdanmicamica bogdan bogdanmica Data 29 martie 2020 19:40:43
Problema Hashuri Scor 0
Compilator c-64 Status done
Runda Arhiva educationala Marime 2.83 kb
#include <stdio.h>
#include "tlg.h"
#include "thash.h"


#define PRIM 1171



typedef int (*TFCmp)(int,int);
typedef int (*TFHash)(int);

typedef struct 
{
	size_t M;
	TFHash fh;
	TLista *v;
}TH;

typedef struct celula
{
	int info;
	struct celula *urm;
}TCelula,*TLista;

typedef int (*TFCmp)(int ,int);
TH *initTH(size_t M,TFHash fh)
{
	TH *h = (TH*)calloc(sizeof(TH),1);
	if(!h)
	{
		printf("eroare alocare\n");
		return NULL;
	}
	h->M = M;
	h->fh = fh;
	h->v = (TLista*)calloc(M,sizeof(TLista));
	if(!h->v)
	{
		free(h);
		return NULL;
	}
	return h;
}

void distrTH(TH **ah)
{
	TLista *p,el,aux;
	int i;
	for(p = (*ah)->v ; p < (*ah)->v + (*ah)->M; p++)
	{
		for(el = *p; el != NULL;)
		{
			aux = el;
			el = el->urm;
			free(aux);
		}
	}
	free((*ah)->v);
	free(*ah);
	*ah = NULL;
}

void afiTH(TH *ah)
{
	TLista p,el;
	for(int i = 0; i < ah->M; i++)
	{
		p = ah->v[i];
		if(p)
		{
			printf("LISTA %d\n",i);
			for(el = p; el != NULL; el = el->urm)
				printf("%d ",el->info);
			printf("\n");
		}
	}
}

int existaTH(TH *a,int nr,TFCmp f)
{
int i;
int cod = a->fh(nr);
TLista el;
for(el = a->v[cod]; el != NULL; el = el->urm)
{
	if(f(el->info,nr) == 0)
		return 1;
}
return 0;
}

int insTH(TH *a,int nr,TFCmp f)
{
	int cod = a->fh(nr);
	TLista el;
	for(el = a->v[cod]; el != NULL; el = el->urm)
		if(!f(el->info,nr))
			return 0;
	int rez = insLista(a->v + cod,nr);
	return rez;
}

int extrTH(TH *a,int nr,TFCmp f)
{
	int cod = a->fh(nr);
	TLista p,prev;

	for(prev = NULL, p = a->v[cod]; p != NULL; prev = p, p = p->urm)
		{
			if(!f(p->info,nr))
			{
				TLista aux = p;
				if(prev)
				prev->urm = p->urm;
				else
					p = p->urm;
				free(aux);
				return 1;
			}
		}
return 0;
}

int insLista(TLista *adrLista,int nr)
{
	TLista aux = malloc(sizeof(TCelula));
	if(!aux)
		return 0;
	aux->info = nr;
	aux->urm = *adrLista;
	*adrLista  = aux;
	return 1;
}

void distruge(TLista *adrLista)
{
	while( *adrLista != NULL)
	{
		TLista aux = *adrLista;
		if(!aux)
			return;
		*adrLista = aux->urm;
		free(aux);
	}
}

void afisare(TLista *adrLista)
{
	if(!*adrLista)
	{
		printf("lista goala\n");
		return;
	}

	for(; *adrLista;*adrLista = &(*adrLista)->urm)
	{
		printf("%d ",(*adrLista)->info);
	}
	printf("\n");
}


int codHash(int nr)
{
	return PRIM % nr;
}

int cmp(int a,int b)
{
	return a - b;
}

int main()
{
	int M = 2000000;
	TH *h = initTH(M,codHash);
	FILE *in,*out;
	in = fopen("hashuri.in","rt");
	if(!in)
		return 1;
	int n;
	fscanf(in,"%d",&n);
	int i;
	out = fopen("hashuri.out","wt");
	for(i = 0;i < n;i++)
	{
		int cmd,nr;
		fscanf(in,"%d",&cmd);
		fscanf(in,"%d",&nr);
		if(cmd == 1)
		{
			insTH(h,nr,cmp);
		}
		else if(cmd == 2){
			if(existaTH(h,nr,cmp)){
				extrTH(h,nr,cmp);
				
			}
		}
		else if(cmd == 3)
			{
				if(existaTH(h,nr,cmp))
					fprintf(out,"1\n");
				else 
					fprintf(out,"0\n");
			}
	}

	fclose(in);
	fclose(out);
	distrTH(&h);
	return 0;
}