Thread: [Dev-C++] C++ Linked Lists and Add After
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Jordan B. <jor...@me...> - 2011-04-24 18:23:46
|
My teacher has given us the following code, within this code I have added the function "void add_after" and am trying to get it to work I do not get an error with this program. It will run but it will not add the word after. I think once you review the code it should be self explanatory: <code> #include <iostream> #include <cstdlib> #include <cstring> using namespace std; const int MAX_WORD_LENGTH = 80; /* definition of a node */ struct Node; typedef Node *Node_ptr; struct Node { char word[MAX_WORD_LENGTH]; Node_ptr ptr_to_next_node; }; /* Function to assign a linked list to "a_node" */ void assign_list(Node_ptr &a_list); /* Function to assign a new dynamic node variable to "a_node" */ void assign_new_node(Node_ptr &a_node); /* Function to print the strings in the list "a_node" */ void print_list(Node_ptr a_node); void add_after(Node_ptr &list, char a_word[], char word_after[]); //This function inserts a node containing "word_after" in the linked list "list", after the first occurrence of a node containing "a_word". If "list" does not contain such a node, the function leaves it unchanged. //***************************************************** void main() { char word_a[MAX_WORD_LENGTH]; char aword[MAX_WORD_LENGTH]; Node_ptr my_list = NULL; assign_list(my_list); cout << "\nTHE LIST IS NOW:\n"; print_list(my_list); cout << "After which word would you like to add a word?:"; cin >> word_a; cout << "What word would you like to add after that word?:"; cin >> aword; add_after(my_list,aword, word_a); cout << "The list is now:"<< endl; print_list(my_list); } //***************************************************** /* DEFINITION OF FUNCTION "assign_list" */ void assign_list(Node_ptr &a_list) { Node_ptr current_node, last_node; assign_new_node(a_list); cout << "Enter first word (or '.' to end list): "; cin >> a_list->word; if (!strcmp(".",a_list->word)) { delete a_list; a_list = NULL; } current_node = a_list; while (current_node != NULL) { assign_new_node(last_node); cout << "Enter next word (or '.' to end list): "; cin >> last_node->word; if (!strcmp(".",last_node->word)) { delete last_node; last_node = NULL; } current_node->ptr_to_next_node = last_node; current_node = last_node; } } /* END OF FUNCTION DEFINITION */ /* DEFINITION OF FUNCTION "assign_new_node" */ void assign_new_node(Node_ptr &a_node) { a_node = new Node; if (a_node == NULL) { cout << "sorry - no more memory\n"; exit(1); } } //***************************************************** /* DEFINITION OF FUNCTION "print_list" */ void print_list(Node_ptr a_node) { while (a_node != NULL) { cout << a_node->word << " "; a_node = a_node->ptr_to_next_node; } } //***************************************************** void add_after(Node_ptr &list, char a_word[], char word_after[]) { Node_ptr currNode; Node_ptr newNode; Node_ptr nextNode; currNode = list; while(currNode != NULL) { if(currNode -> word == a_word) { 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); } else currNode = currNode -> ptr_to_next_node; } } </code> |
From: Michal M. <mol...@se...> - 2011-04-24 20:51:32
|
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. |
From: jordanburnam1990 <jor...@me...> - 2011-04-25 00:10:44
|
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. > |
From: Per W. <pw...@ia...> - 2011-04-25 07:04:12
|
Note that your function is using the same word twice in the code - it isn't bothering about the other word. And also - you claim it should insert the new word after the "first" match - but if the function doesn't return after a match, it will insert the new word after every match. /pwm On Sun, 24 Apr 2011, jordanburnam1990 wrote: > 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. > > > > ------------------------------------------------------------------------------ > Fulfilling the Lean Software Promise > Lean software platforms are now widely adopted and the benefits have been > demonstrated beyond question. Learn why your peers are replacing JEE > containers with lightweight application servers - and what you can gain > from the move. http://p.sf.net/sfu/vmware-sfemails > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
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. |
From: Reid T. <rei...@at...> - 2011-04-25 03:26:50
|
On 4/24/2011 2:23 PM, Jordan Burnam wrote: > if(currNode -> word == a_word) you can't compare strings this way man or google strcmp and friends |