Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
procurei em tudo quanto eh lugar e nao achei um exemplo simples de entender, alguem ja conseguiu implementar o metodo inserir hashing com lista encadeada? tipo esse do codigo
https://www.filepicker.io/api/file/1DNVwJkFSLmheqyHwd41 (imagem do codigo)
Um exemplo?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct __hash
{
char name[16];
int val;
struct __hash* next;
} hash;
inline hash*
_hash ()
{
void* nw = malloc(sizeof(hash));
if(nw == NULL)
{
perror("_hash (malloc)");
abort();
}
return nw;
}
void
ash_ins (hash** hs, char* nam, int val)
{
hash* frs = _hash();
strncpy(frs->name, nam, sizeof frs->name);
frs->val = val;
frs->next = *hs;
*hs = frs;
}
hash*
ash_find (hash hs, char nam)
{
while(strcmp(hs->name, nam) != 0)
if((hs = hs->next) == NULL)
return NULL;
return hs;
}
void
ash_free (hash* hs)
{
hash* aux;
while(hs != NULL)
{
aux = hs->next;
free(hs);
hs = aux;
}
}
int
main (int argc, char** argv)
{
hash* hs = NULL;
ash_ins(&hs, "tmp1", 10);
ash_ins(&hs, "tmp2", 5);
ash_ins(&hs, "tmp3", 15);
ash_ins(&hs, "tmp4", 30);
printf("tmp1 = %d\n", (ash_find(hs, "tmp1"))->val);
ash_free(hs);
return 0;
}
Sim.
Qual sua dúvida?