0

I wanted to give hashtables a go with pset5. I've set the struct up as:

typedef struct node {
    char *data;
    struct node *next;
} node;

Every time I link a new node all of the node->data values are replaced w/the current string. Do I need to malloc for data in addition to node*whatever? Or am I missing something. thanks!

Basically the pseudocode is this for the load part....

char word[26] is "whatever the word is"
if node is null
     malloc a node
     node->data equals word 
     node->next equals null
else if new node isnt null
     create temp node and malloc
     temp->next = new node
     temp->data = next word that is put into function
     node = temp
7
  • you can provide, in pseudocode how your program works?
    – MARS
    Commented Jun 15, 2016 at 19:05
  • Just tried to add some of the pseudocode for the load part. thanks!
    – Michael
    Commented Jun 15, 2016 at 20:23
  • char *data, char word[26], I don't understand. are you using a trie, or hashtable?.
    – MARS
    Commented Jun 15, 2016 at 22:15
  • its a hashtable. maybe I'm doing this wrong? I've got a load function and two arguments. I'm bringing in the node*hashtable[x] array/pointer and bringing in a string. Then assigning that array to the newly malloc'd node in the list that I wanted the hashtable[x] to point to. wrong approach?
    – Michael
    Commented Jun 15, 2016 at 22:31
  • you can send a link to pastebin for example, and I'll look when I have a spare moment
    – MARS
    Commented Jun 15, 2016 at 22:38

1 Answer 1

1

well, no where to start, first see the hash tablenode* hashtable[ALPHA], is an array of pointers to node, are pointers we will use to store our words according to their index hash, to equal hash index equal pointer, for these pointers do not need malloc, your loop to check does not work, possibly segmentation fault. hashtable[k] == NULL, it is unnecessary testing, ultimately I think you need to spend some time reviewing the theoretical part, it seems that your coding is a bit hasty and confused. I recommend going step by step, you need a clear understanding of the concepts before making a relatively long program, because it will be impossible to know what's wrong. First try to understand how a hash table works, then works with linked lists, it helps me do my small test programs, to see easily how it works, is very clarifying. Good luck with pset5, is not easy

EDIT: you're still keeping in your mind a trie, you should read more about hash table, course videos are good, I recommend review them. First, The best way, in my opinion is to define the structure as

typedef struct node
{
    char word[LENGTH + 1];         // len max, plus 1 for \0
    struct node* next;
}
node;

is better than data *, then malloc, because we know in advance the size and we will avoid the mess for free memory.

 node* hashtable[HASHSIZE]; 

is an array of pointers, do not need malloc not point to any valid place (for now), they do not contain any word or will contain. CHECK:

bool check(const char* word)

Here is where the first mistake you commit, you're thinking of a trie, in a hash table we store each particular word, you should not iterate over each individual character

node2* buffer = hashtable[k];

buffer is a pointer to a node data type, like char * buffer is a pointer to a data type character, buffer is only a pointer, contains no words, in C we need to declare each data type.finally I will give the pseudocode to check, I can not elaborate any further and you need to clarify concepts before you start writing code, it is generally not possible to review an entire program and tell you everything that is wrong, because it goes against honor code and at the end you do not learn anything:

// Convert each character to lowercase on a copy, because the function gives a constant string

// Calculate the hash value of the word (every word, not every character)

// We walk the linked list searching of a match

  -declare a pointer for this node type, and assign it to the pointer for
   the "cube" pointed to our index hash:

   node* crawler = hashtable[k];
     while crawler not null
      We compare our copy with the word
       if yes return true
       if not crawler = crawler-> next
       return false

LOAD: Finally, we do not need to do if (hashtable [z] == NULL) ... else ... Although your initializes hash table to null for all possible values, is a waste of time, once we calculate a hash index word, do the necessary operations on the linked list to know where we should point list:

 new_node-> next = hashtable[k];
 hashtable[k] = new_node;

it does not matter that hashtable[k] points to null or garbage values, we have already assigned other values which are not assigned we do not care.I hope that this will begin. Finally your hash table is too simple, because it does not go against honor code I leave a hash function that I wrote for this set of problems in 2014

// Funcion hash
    unsigned int hash_function(char* word)
    {
        int salto = 6;
        // initializes the index to zero, will be the sum of the characters
        unsigned int index = 0;
        // pointer to word
        char* p = word;

        // Add ascii values with Left rotation, independently of capitalita.
        while (*p++)
        { 
            // rotation function
            unsigned int rotl = (((*p) << salto) | ((*p) >> (sizeof(*p)*CHAR_BIT - salto)));
            index = index + rotl;      
        }

        // We get the final hash value by the method of multiplication
        double factor = 0.6180339887,parte_decimal,parte_entera;
        factor = factor*index;
        //Get the decimal part of factor
        parte_decimal = modf (factor, &parte_entera); // 
        unsigned int k = (unsigned int)abs((HASHSIZE*(factor*parte_decimal)));

        // return hash index
        return k%HASHSIZE;
    }
    // End hash function
1
  • Thanks for your help. I had figured out some things since my post & the code is sorta working now (check is super slow) but Im confused a bit about your comment re the hashtable[]. Wouldnt I assign the first node in the linked list to the index hashtable[k]? Id need to malloc for that and then malloc a place for the data to be stored? am i still missing something. Here's my simplified more updated code I'm using in the actual dictionary.c... does this make more sense? pastebin.com/LNFmL2gW ? thanks again for your help. not sure why tries were a lot easier to understand. Thanks!
    – Michael
    Commented Jun 17, 2016 at 15:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .