Re: [Dev-C++] C++ Linked Lists and Add After
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Michal M. <mol...@se...> - 2011-04-25 23:58:52
|
There are two additional errors: 1) As already pointed out by Per, you are using a_word instead of word_after in the condition, so the line: if(currNode -> word == a_word) should be: if (strcmp(currNode -> word, word_after) == 0) 2) The infinite loop is because when you found the word you do not change the currNode so the while(currNode != NULL) never becames true. Using break is correct solution you just have to put it right before the else part: if (strcmp(currNode -> word, word_after) == 0) { assign_new_node(newNode); nextNode = currNode ->ptr_to_next_node; currNode ->ptr_to_next_node = newNode; newNode ->ptr_to_next_node = nextNode; strncpy(newNode->word, a_word, MAX_WORD_LENGTH); break; // HERE } else currNode = currNode -> ptr_to_next_node; } (You can as well do currNode = NULL; instead of the break or use return in this case.) Dne 25.4.2011 2:10, jordanburnam1990 napsal(a): > Now I get an infinite loop when using the compare string functions > for the if statement in add_after any ideas? > > Sent from my iPhone > > On Apr 24, 2011, at 4:51 PM, Michal Molhanec<mol...@se...> > wrote: > >> Dne 24.4.2011 20:23, Jordan Burnam napsal(a): >>> if(currNode -> word == a_word) >> >> You cannot compare strings with ==, you'll have to use strcmp() >> function like it is already used inside the assign_list() >> function. >> > > I saw why I had the infinite loop cause it traversed through the > dynamic memory till it found the end which can take a while. So I > added a break statement after the if statement and that has solved > it. But now it adds the name to the wrong place in the list each > time. I know it's a simple logic error but I just can't seem to pin > point it. |