I've created a hash table and I'm inserting things into it but for some reason,
after I put things on the table when I go to print from the table she makes a wrong print.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
typedef struct hash Hash;
typedef struct utilizador{
char nick[6];
char nome[26];
}
struct hash{
int quantidade, table_size;
utilizador **itens;}
Hash* criateHash(int table_size){
Hash* ha= (Hash*)malloc(sizeof(Hash));
if(ha!=NULL) {
int i;
ha->table_size = table_size;
ha->itens = (utilizador **) malloc(table_size * sizeof(utilizador *));
}
if(ha->itens==NULL) {
free(ha);
return NULL;
}
ha->quantidade=0;
for(int i=0; i< ha->table_size; i++){
ha->itens[i]=NULL;
}
return ha;
}
int string_value(char *string){
int i, valor=0;
int tam= strlen(string);
for(i=0; i< tam; i++){
valor= pow(31,tam-1 )* (int) string[i]+valor;
}
return valor;
}
int hash_function(int chave, int table_size, Hash *ha){
int position=(chave%table_size);
if (ha->itens[position]==NULL)
return position;
else {
for (int j = 0; j < table_size; j++) {
position = position + j;
if(position>table_size)
return -1;
else if (ha->itens[position] == NULL) {
return position;
}
}
return -1;
}
}
int insereHash(Hash* ha, utilizador *a, int table_size){
int i, check, chave;
check=searchHash(ha, a, table_size);
if(check==-1) {
chave=string_value(a[0].nick);
i = hash_function(chave, table_size, ha);
if (i != -1) {
ha->itens[i] = a;
ha->itens[i]->activo=true;
printf("%d %d ",check, i);
printf("+ utilizador %s criado
",ha->itens[i]->nick );
} else if (i==-1)
printf("hash n?o tem espa?o");
}else if (check!=-1){
printf("%d", check);
printf("+ nick %s usado previamente
", a[0].nick);
}
}
int searchHash(Hash* ha, utilizador *a, int table_size){
int chave=string_value(a[0].nick);
int position=(chave%table_size);
if(ha->itens[position]->nick!=NULL) {
if (str_compare(ha->itens[position]->nick, a[0].nick) == 0)
return position;
else {
for (int j = 0; j < table_size; j++) {
position = position + j;
if (position > table_size)
return -1;
else if(ha->itens[position]->nick==NULL)
j++;
else if (str_compare(ha->itens[position]->nick, a[0].nick) == 0) {
return position;
}
}
}
}
return -1;
}
void input(utilizador *a, char *comando, char *aux) {
fgets(aux, 35, stdin);
sscanf(aux, "%s %s %99[^
] ", comando, &a[0].nick, &a[0].nome);
}
int str_compare( char *string1, char *string2){
for(int i=0;i<5;i++) {
if (string1[i] != string2[i]) {
return 1;
}
}return 0;
}
int table_size=23;
utilizador ar_temp[1];
char comando[2];
char aux[35];
int pos;
Hash *ha;
int main() {
ha = criateHash(table_size);
while((strcmp (comando, "X"))!=0 && (strcmp (comando, "x")!=0)) {
input(ar_temp, comando, aux);
if ((strcmp (comando, "U")==0)) {
insereHash(ha, ar_temp, table_size);
}
}
for(int g=0; g<table_size; g++){
printf("%s ",ha->itens[g]->nick);
}
return 0;
}
the input tha i use is:
U asdre ana
U qwert joa
U qwert pedro
U hjklm gomes
U ertyu ana
U hhudr humberto
U e5sxs wis
U 2kkji toba
U p07hm rojao
U zxcvb tutu
x
and my output is:
-1 19 + utilizador asdre criado
-1 22 + utilizador qwert criado
22+ nick qwert usado previamente
-1 10 + utilizador hjklm criado
-1 11 + utilizador ertyu criado
-1 20 + utilizador hhudr criado
19+ nick e5sxs usado previamente
-1 7 + utilizador 2kkji criado
-1 5 + utilizador p07hm criado
10+ nick zxcvb usado previamente
10+ nick zxcvb usado previamente
(null) (null) (null) (null) (null) zxcvb (null) zxcvb (null) (null) zxcvb zxcvb (null) (null) (null) (null) (null)
(null) (null) zxcvb zxcvb (null) zxcvb
Process finished with exit code 0
My problem is: why the other values I inserted earlier in the table are not there and how can I resolve this?
Can you help me?
See Question&Answers more detail:
os