0

I am working on a C program that requires me to create a list and some functions(which I've done) and then implement a hash table like this. The program takes as input words, which are already rated by their positivity(0-4), 0 is negative etc.

typedef struct {
int size;
int num_entries;
entryT *table;
} hashT; 

The hash table must have at least these functions:

  1. A fuction which takes as parameters a word and its score, and finds in which list the word must be inserted (uses the insert function).
  2. A function that prints the hash table.
  3. A rehash function.
  4. One that destroys the hast table.
  5. A hash function which Im given. It returns and int, and divided by the table size, returns the position where the word must be stored.

Here are the functions of the list that I've implemented.

    struct Node {
      struct Node *next;
      char* word;
      int count;
      int score;
    } *head;

    int AddtoList(char *lexi,int skor) {
      struct Node *add = (struct Node*)malloc (sizeof(struct Node*));
      if (add == NULL) {
        exit(1);
      }
    add = head;
    int found = 1;;
    while (strcmp(add->word, lexi) != 0) {
      add = add->next;
      found=0;
    }
    if (found == 1) {  //uparxei hdh sti lista
       add->count++;
       add->score;
       return 1;
     }
    else {
    struct Node *temp = (struct Node*)malloc (sizeof(struct Node*));
    if (temp == NULL) exit(23);
    strcpy(temp->word,lexi);
    temp->score = skor;
    temp->count++;

    if (head == NULL) { //adeia lista
        *head = *temp;
        head->next = NULL;
    }
    else {
        temp->next = head;
        *head = *temp;
    }
     return 1;
    }
    }   

    void Print() {
      struct Node *temp;
      temp = head;
      if (temp == NULL) {
        printf("Empty List-> Exiting\n");
      }
      while (temp != NULL) { 
        if (temp->next != NULL) {//an o epomenos komvos den einai teleftaios
            printf("[ ");
            printf("%c %d 0.2%d", temp->word, temp->count, temp->score);
            printf("]");
            printf(", ");
        }
        else {
            printf("[ ");
            printf("%c %d 0.2%d", temp->word, temp->count, temp->score);
            printf("]");
            printf("\n");   
        }

        temp = temp->next;
      } 
    }

    void DeleteList(struct Node *head) {
      struct Node *current = head;
      struct Node *next;

      while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
     }

      head = NULL;
     }

     char* FindNode(char *lexi) {
       struct Node *current = head;
       struct Node *next;
       while(strcmp(current->word, lexi) != 0) {
         next = current->next;
       }
     return (current->word);
     }

    struct hashT{
      int size;
      int num_entries;
      struct Node table;
    } *hash;

What I want is any help I can get with the hash table functions. Please let me know if you need me to post anything else about the program.

Thank you!

Cookie
  • 1
  • 1
  • 1
    `malloc (sizeof(struct Node*));` That might be too small ... And: you should *not* cast malloc()s return value. – joop Jan 03 '17 at 13:08
  • You need to allocate space for a `Node` `struct`, not a pointer to a `Node` `struct`. It is better practice to write this as: `struct Node *add = malloc(sizeof(*add));`. 1) no need to cast result of `malloc()`; 2) easier to write correctly, read, and maintain when name of pointer to allocation is used instead of explicit type. – ad absurdum Jan 03 '17 at 15:03
  • The provided source doesn't describe a hashtable but only how to manage elements having the same hashvalue by using a linked-list. To have a complete hashtable, the `struct hashT` shall store the `score` shared by all nodes for each items of the hashtable. – J. Piquard Jan 03 '17 at 15:42
  • 1
    Well, your task is to design an effective hash function and a rehash in case of collisions. You've already mentioned that your hash function should take as parameters the word and its score. What is the aim of your hash function? I mean there must be a rational in order to draw a "good" hash function. How would you like to discriminate your words based on their score and their letters? – P. Str Jan 03 '17 at 16:38
  • The hash table will contain words based on how "positive" they are. My input file is a film rating.Its first word is an int :0,1,2,3,4 based on how positive it is. The rest is just words.Then I have to collect all the words, check if each w0rd is on the hash table, if not i aill add it there and the load factor will change. When it exceeds a limit I'll have to rehash. Im having trouble with this – Cookie Jan 03 '17 at 20:05

0 Answers0