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:
- 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).
- A function that prints the hash table.
- A rehash function.
- One that destroys the hast table.
- 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!