HUFFMAN.c



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>


struct NODO
{
	char CAR;
	int FREQ;
	struct NODO *sig;
};

struct NODO *INICIO = NULL;

main()
{
	int k=0;
	int CONT[27];
	char *cad = (char*)malloc(100);

	strcpy(cad, "Mecanismo del algoritmo");
	while( cad[k] ) { cad[k] = toupper(cad[k]); printf("%c", cad[k]); k++; }
	printf("\n");
	for(k=0; k<26; k++) CONT[k] = 0;
	k=0;
	// PASO 1
	while( cad[k] )
	{
		if (cad[k] == 32) CONT[26]++;
		else CONT[cad[k] - 'A']++;
		k++;
	}

	for(k=0; k<26; k++) printf("%c\t%i\n", (k+'A'), CONT[k]);
	printf("ESPACIOS\t%d\n", CONT[26]);

	// PASO 2
	for(k=0; k<27; k++)
	{
		if ( CONT[k] > 0 )
		{
			struct NODO *NEW = (struct NODO*)malloc(sizeof(struct NODO));
			NEW->CAR = k+'A';	NEW->FREQ = CONT[k];	NEW->sig = NULL;

			if ( !INICIO ) INICIO = NEW;
			else {
				NEW->sig = INICIO;
				INICIO = NEW;
				}
		}
	}

	while ( INICIO )
	{
		printf("%c\t%d\n", INICIO->CAR, INICIO->FREQ);
		INICIO = INICIO->sig;
	}

}