3
\$\begingroup\$

I'm pretty new to C and I'm trying to implement a hashtable with chaining for a struct I created:

HASH.h:

#ifndef HASH_H
#define HASH_H
#include <stdio.h>
#include <stdlib.h>
#define MAXNAME 8000

/* Utilizing separate chaining and hashtables to
 * save the informations about objectives
 */

typedef struct {
   unsigned long int id, duration;
   char name [MAXNAME]; 
   unsigned long int *dep;
}Objective

typedef struct node{
   Objective objective;
   struct node*next;
}*link

void hashInit(int m);
link insertBegin(link head, Objective obj);
void objInsert(Objective obj);

#endif

HASH.c:

#include "HASH.h"
#include <stdlib.h>
#include <stdio.h>

static link *heads;
static int M;

int hash(Key value, int m){
   return value%m;
}

void hashInit(int m){
   int i;
   M = m;
   heads = (link*)malloc(M*sizeof(link));
   for(i = 0; i < M; i++)
       heads[i] = NULL;
   }

link insertBegin(link head, Objective obj){
    link new = (link)malloc(sizeof(struct node));
    new->value = obj;
    new->next = head;
    return new;
}

void objInsert(Objective obj){
   int i = hash(key(obj), M);
   heads[i] = insertBegin(heads[i], obj);
}

I'm still in the very beginning and don't have much but I'm just looking for advice on how and if I can improve what I did here, what I might have done wrong, and if I'm on the right path.

\$\endgroup\$

closed as off-topic by ChrisWue, Toby Speight, Stephen Rauch, Sᴀᴍ Onᴇᴌᴀ, Mast May 15 '18 at 17:16

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – ChrisWue, Toby Speight, Stephen Rauch, Sᴀᴍ Onᴇᴌᴀ, Mast
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Does this even compile? Those typedef declarations aren't terminated. \$\endgroup\$ – Toby Speight May 15 '18 at 10:12

Browse other questions tagged or ask your own question.