This is my first C program that does something useful. I'm implementing a generic hash table that can take any type of key and item. But I'm getting errors I don't know how to fix:
hash.h:32: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘hash_create’ hash.h:38: error: expected ‘)’ before ‘hash_func’ EDIT: Aftertypedeffing struct hash_t to hash, I am getting tons of these errors. hash.c:12: error: invalid type argument of ‘->’ (have ‘hash’)#ifndef _HASH_H
#define _HASH_H
/* Definitions for abstract hashtable. */
typedef void * key;
typedef void * item;
typedef void * hash_func;
typedef void * compare_func;
struct pair {
key k;
item i;
};
struct hash_t {
int size;
int max_size;
int array[16];
hash_func hf;
compare_func cf;
};
typedef int boolean_t;
#ifndef FALSE
#define FALSE 0
#define TRUE (!FALSE)
#endif // FALSE
/*
* Creates and returns a new hashtable.
* Returns null on failure, else a valid hashtable.
*/
extern hash_t hash_create();
/*
* Sets the hash function to the new value if hash is empty.
* Returns true if set successfully, false if nothing changed.
*/
extern boolean_t hash_set(hash_t, hash_func pf);
/* Sets the function used to compare values.
* Returns true if set successfully, false if nothing changed.
*/
extern boolean_t hash_compare(hash_t h, hash_func pf);
/*
* Returns TRUE if hashtable is empty. FALSE otherwise.
*/
extern boolean_t hash_is_empty(hash_t h);
/*
* Returns number of elements in the t开发者_运维问答able.
*/
extern int hash_size(hash_t h);
/*
* Adds the key and value pair to hashtable.
*/
extern void hash_add(hash_t h, pair p);
/*
* Returns the pair associated with the given key.
*/
extern pair hash_lookup(hash_t h, key k);
/*
* Returns true if key is present, else false.
*/
extern boolean_t hash_is_present(hash_t h, key k);
/*
* Removes the pair associated with given key.
* Returns true if successfully removed, else false.
*/
extern boolean_t hash_remove(hash_t h, key k);
#endif //_HASH_H
</pre></code>
In case you want to look at the hash.c as well as the previous hash.h:
<pre><code>
/* Implements hash abstract data type. */
#include assert.h> //can't include < in post or it disappears
#include stdio.h> //can't include < in post or it disappears
#include stdlib.h> //can't include < in post or it disappears
#include "hash.h"
hash_t
hash_create()
{
hash_t h = (hash_t)malloc(sizeof(struct hash_t));
h->size = 0;
}
boolean_t
hash_set(hash_t, hash_func pf){
if(!hash_is_empty)
return FALSE;
else
hf = pf;
return TRUE;
}
boolean_t
hash_set(hash_t, hash_func pf){
if(!hash_is_empty)
return FALSE;
else
cf = pf;
return TRUE;
}
boolean_t
hash_is_empty(hash_t h){
return h->size==0;
}
int
hash_size(hash_t h){
return h->size;
}
void
hash_add(hash_t h, pair p) {
int i = h->hash_func(max_size, p->key);
if(h->array[i]!=NULL)
i++;
h->array[i] = p;
h->size++;
if(h->size > .75*h->maxsize) {
h->max_size *= 2;
int arr[h->max_size];
int* temp = h->array;
h->array = arr;
int i = 0;
for(i; i < h->max_size / 2; i++) {
if(temp[i]!=NULL)
hash_add(h,temp[i]);
}
}
pair
hash_lookup(hash_t h, key k) {
int i = h->hash_func(max_size, k);
while(h->array[i]!=NULL && h->compare_func(h->array[i]->key,k)!=0) {
i++;
}
return h->array[i];
}
boolean_t
hash_is_present(hash_t h, key k) {
return (hash_lookup(h,k)!=NULL);
}
boolean_t
hash_remove(hash_t h, key k) {
pair p = hash_lookup(h,k);
if(p == NULL)
return FALSE;
else
*p = NULL;
return TRUE;
}
Since you're writing C you need to do:
extern struct hash_t hash_create();
hash_t
isn't a type - struct hash_t
is. This applies to all other locations where you use hash_t
as well - use struct hash_t
instead.
Alternatively:
typedef struct hash_t {
int size;
int max_size;
int array[16];
hash_func hf;
compare_func cf;
} hash;
extern hash hash_create();
精彩评论