Hash table question, code posted (~40ines)
From: exonic (exonic_at_triton.net)
Date: 03/31/04
- Next message: Kenneth Brody: "Re: Reading program output from stdin"
- Previous message: pete: "Re: Delimiting problems"
- Next in thread: exonic: "Re: Hash table question, code posted (~40ines)"
- Reply: exonic: "Re: Hash table question, code posted (~40ines)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 31 Mar 2004 12:23:33 -0500
I am having some trouble with a hash table routine, I have
a few problems with figuring out why I can't access the table in
the hash_t structure. And weather or not I have a decent hashing
function written. I am simply using modulus. This project is a
test to help me understand the 5W's of hashing..
Here are my structs
--------------------
#ifndef HAVE_HASH_H
#define HAVE_HASH_H
#include <stdlib.h>
#include <stdio.h>
typedef struct {
unsigned int value;
} htable_t;
typedef struct {
size_t size;
htable_t *table;
} hash_t;
/* prototypes */
hash_t *hash_init(hash_t *h, size_t size);
int hash_index(hash_t *h, unsigned int value);
int hash_add(hash_t *h, unsigned int value);
int hash_find(hash_t *h, unsigned int value);
int hash_print(hash_t *h);
int hash_free(hash_t *h);
#endif
Here is my code
-------------
hash_t *hash_init(hash_t *h, size_t size) {
htable_t *table;
h = malloc(sizeof(hash_t));
if (!h) return NULL;
h->size = size;
table = malloc(sizeof(htable_t) * size);
if (!table) return NULL;
h->table = table;
printf("hash_init(): struct pointer %p, size %d %d\n", h, size, h->size);
return h;
}
int hash_index(hash_t *h, unsigned int value) {
int i, size;
size = h->size;
i = (int) value % size;
return i;
}
int hash_add(hash_t *h, unsigned int value) {
int size, i;
htable_t *table;
size = h->size;
table = h->table;
i = hash_index(h, value);
printf("hash_add(): struct pointer %p, index %d\n", h, i);
/*
Here is where I can't figure out how to access the table, I thought this
was valid but doesn't seem to work.
table[i]++;
*/
return 0;
}
int hash_find(hash_t *h, unsigned int value) {
return 0;
}
int hash_print(hash_t *h) {
int i,size;
htable_t *table;
table = h->table;
size = h->size;
printf("hash_print(): struct pointer %p, size %d\n", h, size);
for(i=0; i<size; i++) {
/* I don't know if I am acessing the table correctly here either */
printf("table[%d] = %d\n", i, (int) table[i]);
}
return 0;
}
int hash_free(hash_t *h) {
free(h->table);
free(h);
return 0;
}
- Next message: Kenneth Brody: "Re: Reading program output from stdin"
- Previous message: pete: "Re: Delimiting problems"
- Next in thread: exonic: "Re: Hash table question, code posted (~40ines)"
- Reply: exonic: "Re: Hash table question, code posted (~40ines)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|