Prof. Dr. R. Laue                                                                                                                                  SS01
                                Informatik II
                                Übungsblatt 10

Lösung Aufgabe 29

#include <stdlib.h>
struct hashentry {
        char * text;
        struct hashentry *next;
        };
struct hashentry * hashtafel[1024];

hmitte(char *text)
{
int l = strlen(text)/2;
unsigned short summe=0;
for (;l>=0;l--)
        summe += ((unsigned short*)text)[l];

summe = summe * summe;
summe = summe * 8;
summe = summe / 64;
return summe;
}

insert_hashtable(char *text, int adresse)
{
        if (hashtafel[adresse] == NULL)
                {
                hashtafel[adresse] = (struct hashentry *) calloc(1,sizeof(struct hashentry));
                hashtafel[adresse] -> text = text;
                hashtafel[adresse] -> next = NULL;
                return 0;
                }
        else {
                struct hashentry *zeiger = hashtafel[adresse];
                while (zeiger->next != NULL)
                        zeiger = zeiger->next;
                zeiger->next = (struct hashentry *) calloc(1,sizeof(struct hashentry));
                zeiger->next -> text = text;
                zeiger->next -> next = NULL;
                return 0;
        }
}
print_hashtable()
{
        int i;
        for (i=0;i<1024;i++)
                if (hashtafel[i] == NULL) printf("%d: NULL\n",i);
                else    {
                        struct hashentry *zeiger = hashtafel[i];
                        printf("%d:",i);
                        do { printf("%s ",zeiger ->text);
                        zeiger = zeiger->next; }
                        while (zeiger != NULL) ;
                        printf("\n");
                        }
}

main()
{
int i,erg;
for (i=0;i<1024;i++)
        hashtafel[i] = NULL;

again:
    {
    char *text;
    text = (char *)calloc(100,sizeof(char));
    erg = scanf("%s",text);
    if (erg != 1) goto ende;
    printf("%s %hu\n",text,erg = hmitte(text));
    insert_hashtable(text,erg);
    goto again;
    }
ende:

print_hashtable();
;
}