cannot get the hash function in c to work

Hi experts, I'm doing an assignment on hash tables and require some advise

I have the following structs:

typedef struct bucket *ListPtr;
typedef struct conListNode *ListNodePtr;
typedef struct ipaddr
{
  int digit1, digit2, digit3, digit4;
}IPAddress;
typedef struct bucket
{
  int           size;
  ListPtr       listPtr;
}Bucket;
typedef struct conListNode
{
  IPAddress     ipAddress;
  int           portNumber;
  ListNodePtr   next;
}ConListNode;
typedef ListPtr BucketPtr;
typedef struct connctnList
{
  int         tableSize_N;
  int         bucketThreshold_m;
  int         primeNo1_k1;
  int         primeNo2_k2;
  int         primeNo3_k3;
  int         primeNo4_k4;
  int         primeNo5_k5;
  BucketPtr   *bucketPtr;
}connctnList;

To initialise the table, I have the following function:

int makeConnectionList(connctnList *conList)
{
  int counter;
  conList = malloc(sizeof(connctnList));
  if(conList == NULL)  {    return FAILURE;  }

  conList->tableSize_N = TABLE_SIZE;
  conList->bucketThreshold_m = INITIALISE;
  conList->primeNo1_k1 = K1;
  conList->primeNo2_k2 = K2;
  conList->primeNo3_k3 = K3;
  conList->primeNo4_k4 = K4;
  conList->primeNo5_k5 = K5;

  conList->bucketPtr = malloc(sizeof(Bucket) * conList->tableSize_N);

  if (conList->bucketPtr == NULL)  {    return FAILURE; }

  for (counter = 0; counter < conList->tableSize_N; counter++)
  {
    conList->bucketPtr[counter] = malloc(sizeof(Bucket));
    if(conList->bucketPtr[counter] == NULL)    {
      return FAILURE;
    }
    else    {
      conList->bucketPtr[counter]->listPtr = NULL;
    }
  }
  return SUCCESS;

This function to call the hash function to calculate position within array is:

int addConnection(connctnList *conList, IPAddress address, int port)
{
  int hashValue = INITIALISE;
  hashValue = hashCalculation (conList, address, port);
  printf("%d\n", hashValue);
  return 0;
}

And this is the hash function:

int hashCalculation(connctnList *conList, IPAddress address, int port)
{
   int hashValue = INITIALISE;

  hashValue = ((conList->primeNo1_k1 * address.digit1) + \
              (conList->primeNo2_k2 * address.digit2) + \
              (conList->primeNo3_k3 * address.digit3) + \
              (conList->primeNo4_k4 * address.digit4) + \
              (conList->primeNo5_k5 * port)) % \
               conList->tableSize_N;
  return hashValue;
}

Can someone please explain to me why the hashValue printed out is rubbish.

Thanks a million from a very confused person.
kwyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
pratap_rConnect With a Mentor Commented:
yup thats exactly what you need to do. that is what i had mentioned in my reply .. change the * to ** in input parameter :-)

in your main function you will have to change the declaration of connctnList conList; to connctnList *conList; because you are allocating memory in your make connection function. but when you declare connctnList conList; your memory is already allocated! if you are not allowed to change your parameter list then you should remove the malloc inside your makeconnection function..

your new make connection will be
int makeConnectionList(connctnList *conList)
{
  int counter;
  if(conList == NULL)  {    return FAILURE;  }
  conList->tableSize_N = TABLE_SIZE;
.....
...
}
 
nothing else needs to be changed... :-)  rest of the code is fine. the malloc was basically allocating a memory but your main's conlist was not getting it properly...

enjoy!
Pratap
0
 
pratap_rCommented:
can you post the code from where you are calling makeConnectionList ??

as far as i can tell your connection object is not passed properly

try changing to
int makeConnectionList(connctnList **b)
{
connctnList *conList;
conList = (connctnList*)malloc(sizeof(connctnList));
*b=conList;
......
.....
}

main()
{
      connctnList *conList;
      makeConnectionList(&conList);
      ipaddr ip;
      ip.digit1=127;ip.digit2=0;ip.digit3=0;ip.digit4=1;
      addConnection(conList,ip,100);
}

using a table size of 100 i got the hash as 61 --> seems to work!!?

enjoy!
pratap
0
 
pratap_rCommented:
oh and i used 2,3,5,7,11 as values for K1, k2, k3, k4, k5 since i dint know what values you were using.. but i dont see any problem with that
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
kwyAuthor Commented:
Hi pratap

Because this is an assignment, I cannot change the function parameters.  Please see below the extracts from main where the functions are called, your advise is much appreciated :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "connctnList_HT.h"
#include "connctnList.h"

int main (int argc, char **argv)
{
  connctnList conList;

  IPAddress ipAddress1;
  IPAddress ipAddress2;
  -------------etc-----------------
 if (ParseLine(line, &tempCommand, tempIPAddress, tempPortOrIP) == FALSE)
    {      printf("\nError on Line Number %d in file %s", lineNum, argv[FILENAME]);    }
 else
 {
      if (tempCommand == 'C')
      {
        if (makeConnectionList(&conList) == FALSE)
        {         FatalError("Unable to create list !!");        }
      }
      else if (tempCommand == 'A')
      {
        sscanf (tempPortOrIP, "%d", &port);

        if (ParseIP(tempIPAddress, &ipAddress1) == FALSE)
        {     printf("Unable to create IPAddress structure");        }
        if (addConnection(&conList, ipAddress1, port) == FALSE)
        {     FatalError("Unable to add node !!");       }
      }
     -----etc

Thanks again.
0
 
suryaxchangeCommented:
In the following declararation,
  int makeConnectionList(connctnList *conList)
connctnList *conList is wrong.

Use connctnList **conList, because you are allocating memory for conList in the function i.e., you are modifying the pointer so, you need to send the pointer to this pointer same as you send address of integer to modify the value of that integer.

Try this you will get succeeded
Surya.
0
 
kwyAuthor Commented:
Thanks guys for your help, especially you Pratap.
The program is now working beautifully!
kwy
0
 
pratap_rCommented:
with pleasure :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.