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

#include "llist.h"
#include "hashtable.h"

llist *create_hash_table(int m)
{
    int i;
    llist *H;
    H = (llist *) malloc(m * sizeof(llist));
    for (i = 0; i < m; i++) H[i].head = (elist*) 0;
    return H;
}

int hash(const char *s, int m)
{
    unsigned long int h = 0;
    int i, c, n = strlen(s);
    for (i = 0; i < n; i++) {
	c = s[i] - 'a';
	h += (c * c);
    }
    return h % m;
}


void process_string(llist *H, char *s, int m)
{
    int h = hash(s, m);
    elist *e, *x = list_search(&H[h], s);

    if (x == (elist*) 0) {
	e = new_element(s);
	e -> next = H[h].head;
	H[h].head = e;
    }
    else {
	x -> count++;
    }
}

void print_hash_table(llist *H, int m)
{
    elist *x;
    int i;
    for (i = 0; i < m; i++) {
	x = H[i].head;
	while (x != (elist*) 0) {
	    printf("%7d %s\n", x -> count, x -> key);
	    x = x -> next;
	}
    }
}


void summarize_hash_table(llist *H, int m)
{
    elist *x;
    int i, count;
    for (i = 0; i < m; i++) {
	x = H[i].head;
	count = 0;
	while (x != (elist*) 0) {
	    count++;
	    x = x -> next;
	}
	printf("%7d %7d\n", i, count);
    }
}


