Hello, I'm doing a college work on Hash table and I'm having some difficulties. I'll try not to complicate too much (to tell you the truth, running my code helps me understand what I want to do).
Basically, I have, for now, a program that generates strQty random words of random size from 1 to 10 characters. After generating this random word, I enter at the end of the l dynamic list. Until then there is no secret, but here comes my difficulties, and that is where I ask for your help.
The GetValuesList function receives the l dynamic list, and it iterates word by word. For each word, it is iterated letter by letter. I do this to get a value that I called val , which is the sum of the integer values of each character of a word.
With this value, I call the function getValorHash (remembering that the job is about hash table). This function will return me, according to the value of val , a value between 0 and 31 , which is the size of my hash table (M_SIZE).
And now let's start joking. I have a word and a hash value. Let's assume now that I have a hashTable vector of size M_SIZE , I need to verify that the hashTable hashTable [hash] ) is null . If it is, I'll create a new dynamic word list.
So that's one of my biggest doubts, as I dynamically create lists dynamically.
Continuing, assuming I've created a new dynamic list. Your first element will be the word word . And besides, I need to save the memory address of the head of this new list in the hashTable vector in the hash position.
Another important question: What kind of vector is this? Is it an address vector? of pointers? And how do I create this?
Continuing to run the program, the algorithm is quite simple. I'm going to generate a hash for a new word , and check hashTable is null. If it is, I start a new dynamic list, if it is not, it's because its value is the leading address of a dynamic list already created, and word will be added at the end of it.
My question in this part would be how do I, from that value, which is a pointer or address ( do not know very well the difference between these two terms ), access the dynamic list. / p>
I have a very clear idea of how the hash tables work and how my program should work, but my doubts are merely technical about coding all of that.
In fact, I thank you for your patience in helping me. Next, follow my code so far.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <locale.h>
#define STRLIMIT 10
#define M_SIZE 31
#define STR_QTY 100
typedef struct lista {
char word[STRLIMIT];
struct lista* prox;
} Lista;
Lista *insereFim(Lista* l, char word[], int size) {
Lista* novo = (Lista*) malloc(sizeof(Lista));
int i;
for (i=0; i<size; i++) {
novo->word[i] = word[i];
}
novo->word[size] = '#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <locale.h>
#define STRLIMIT 10
#define M_SIZE 31
#define STR_QTY 100
typedef struct lista {
char word[STRLIMIT];
struct lista* prox;
} Lista;
Lista *insereFim(Lista* l, char word[], int size) {
Lista* novo = (Lista*) malloc(sizeof(Lista));
int i;
for (i=0; i<size; i++) {
novo->word[i] = word[i];
}
novo->word[size] = '%pre%';
novo->prox = NULL;
if (l == NULL) {
return novo;
}
Lista* aux = l;
while (aux->prox != NULL) {
aux = aux->prox;
}
aux->prox = novo;
return l;
}
int obterValorHash(int n) {
int hash;
hash = (5 * n) % M_SIZE;
return hash;
}
void obterValoresLista(Lista* l) {
int i, val, hash;
int *table[M_SIZE];
for (i=0; i<M_SIZE; i++) {
table[i] = NULL;
}
do {
val = 0;
for (i=0; i<strlen(l->word); i++) {
val += (int)l->word[i];
}
hash = obterValorHash(val);
printf("%10s\t%d\t%d\t%p\n", l->word, val, hash, &l->word);
l = l->prox;
} while (l != NULL);
}
int main() {
srand(time(NULL));
setlocale(LC_ALL, "");
int strQty = 100;
int strLimit = 11;
int ascMin = 97;
int ascMax = 122;
// Gerando strings aleatorias
int i, j;
int size, letter;
Lista* l = NULL; //Lista encadeada
for (i=0; i<strQty; i++) {
// Tamanho da palavra words[i] -- de 1 a 10.
size = rand()%(10)+1;
char word[strLimit];
for (j=0; j<size; j++) {
letter = rand()%(ascMax-ascMin)+ascMin;
word[j] = (char)letter;
}
// Indica o fim da string
word[size] = '%pre%';
l = insereFim(l, word, size);
}
int *hashArray[strQty];
obterValoresLista(l);
}
';
novo->prox = NULL;
if (l == NULL) {
return novo;
}
Lista* aux = l;
while (aux->prox != NULL) {
aux = aux->prox;
}
aux->prox = novo;
return l;
}
int obterValorHash(int n) {
int hash;
hash = (5 * n) % M_SIZE;
return hash;
}
void obterValoresLista(Lista* l) {
int i, val, hash;
int *table[M_SIZE];
for (i=0; i<M_SIZE; i++) {
table[i] = NULL;
}
do {
val = 0;
for (i=0; i<strlen(l->word); i++) {
val += (int)l->word[i];
}
hash = obterValorHash(val);
printf("%10s\t%d\t%d\t%p\n", l->word, val, hash, &l->word);
l = l->prox;
} while (l != NULL);
}
int main() {
srand(time(NULL));
setlocale(LC_ALL, "");
int strQty = 100;
int strLimit = 11;
int ascMin = 97;
int ascMax = 122;
// Gerando strings aleatorias
int i, j;
int size, letter;
Lista* l = NULL; //Lista encadeada
for (i=0; i<strQty; i++) {
// Tamanho da palavra words[i] -- de 1 a 10.
size = rand()%(10)+1;
char word[strLimit];
for (j=0; j<size; j++) {
letter = rand()%(ascMax-ascMin)+ascMin;
word[j] = (char)letter;
}
// Indica o fim da string
word[size] = '%pre%';
l = insereFim(l, word, size);
}
int *hashArray[strQty];
obterValoresLista(l);
}