예제 #1
0
int main() {
	FILE *f = stdin;
	// f = fopen("/home/Ondra/ahoj1.txt", "r"); // smazat
	char string[MAX_LEN];
	int len;

	htable_t* table = htable_init(HTABLE_SIZE);
	htable_listitem *item;

	while ( (len = fgetword(string, MAX_LEN, f)) != 0 ) {
		if((item = htable_lookup(table, string)) == NULL) {
			fprintf(stderr, "Chyba pri alokaci pameti.\n");
			return EXIT_FAILURE;
		}
	}

	// fclose(f);	//smazat

	htable_iterator b = htable_begin(table);
	htable_iterator *bg = &b;
	htable_iterator end = htable_end(table);

	while( !htable_iter_eq(bg, &end) ) {
		if(htable_iter_deref(bg) == NULL)
			break;
		printf("%s\t%d\n", bg->ptr->key, bg->ptr->data);
		bg = htable_iter_next(bg);
	}
	if(end.ptr != NULL)
		printf("%s\t%d\n", end.ptr->key, end.ptr->data);
	// Uklid haldu
	htable_free(table);

	return EXIT_SUCCESS;
}
/**
 * Zaciatok programu
 */
int main(void)
{	
	Th_table *htable = htable_init(TABLESIZE);
	if (htable == NULL)
		printError("Nepodarilo sa alokovat pamet pre hash table!", htable);
	
	//alokujem si do ktoreho budem ukladat slova z funkcie fgetword
	char *word = (char *)calloc(WORDLENGTHMAX, sizeof(char));	
	if (word == NULL)
		printError("Nepodarilo sa alokovat pamet pre pole!", htable);
	
	Thtable_listitem *item = NULL;
	
	//nacitanie slov do hash table
	while(fgetword(word, WORDLENGTHMAX, stdin) != 0)
	{
		item = htable_lookup(htable, word);
		if (item == NULL)
		{
			free(word);
			printError("Nepodarilo sa alokovat pamet pre novu polozku!", htable);
		}
		
		//pocita vyskity slova
		item->data++;
	}
	//vypis slovo pocetVyskitov
	/////////////////////////////////////////////
	//nastavim iterator na prvu polozku
	Thtable_iterator iteratorBegin = htable_begin(htable);
	//nastavim iterator na poslednu polozku
	Thtable_iterator iteratorEnd = htable_end(htable);
	//vypis key  data
	while (htable_iter_eq(iteratorBegin, iteratorEnd) == false)
	{
		item = htable_iter_deref(iteratorBegin);
		printf("%s\t%d\n", item->key, item->data);
		iteratorBegin = htable_iter_next(iteratorBegin);
	}
	
	//uvolnit alokovane polia..
	htable_free(htable);
	free(word);
	
	return EXIT_SUCCESS;
}