0

I can't seem to break out of this infinite loop here is my pseudo code:

malloc cursor
malloc new_node
for(length of word)
{ 
  if(letter is uppercase)
  {
    make letter uppercase
  }
}
copy string of of word to new_nod->word
hash it
make cursor = table[hash]
while (cursor is not NULL) //here is where the infinite loop begins
{
  if( cursor->word = new_node->word) //this is using strcmp
  {
    return true
    make cursor = cursor->next;
  }
}

in my load function I use the following to avoid collisions:

while(end of file not yet reached)
{
  get word
  hash it
  if(table[hash] is not = to NULL)
  {
    new_node->next = table[hash]
  }
  table[hash] = new_node;
}

EDIT 0: Following, @Kareem 's advice, I have made one minor change, but I still cannot fix the problem. here is my modified code

         malloc cursor
malloc new_node
for(length of word)
{ 
  if(letter is uppercase)
  {
    make letter uppercase
  }
}
copy string of of word to new_nod->word
hash it
make cursor = table[hash]
while (cursor is not NULL) //here is where the infinite loop begins
{
  if( cursor->word = new_node->word) //this is using strcmp
  {
    return true

  }
  make cursor = cursor->next;
}

EDIT 1: It seems to me that cursor is not changing even though I tell it to equal cursor->next!

1
while (cursor is not NULL) //here is where the infinite loop begins
{
  if( cursor->word = new_node->word) //this is using strcmp
  {
    return true
    make cursor = cursor->next;
  }
}

What if cursor->word is NOT equal to new_node->word? cursor will never become cursor->next and thus it's never becoming NULL, right?

2
  • please see my edits – user1475 Sep 28 '14 at 1:26
  • 1
    @user1475 everything seems okay to me except that you don't really need all of these node pointers nor to allocate memory for them. You're not inserting a new word, you're just using cursor to traverse through the already existed words. You also don't need new_node nor to allocate memory for it. You could just use a char array and compare cursor->word with this char array. What makes you sure that this is the infinite loop? – Kareem Sep 28 '14 at 4:45

You must log in to answer this question.

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