Thread: [Dev-C++] Linked List using Pointers - Not adding word correctly
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Jordan B. <jor...@me...> - 2011-04-26 19:02:31
|
Hi everyone, I have the following program that I have created and am trying to get this work correctly. The purpose of this program is to make a litst from the users input then allow the user to add a word after a word in the list. So far this list only adds the word after the first word each time. I have looked at it and ad peers online to view it and have advised me that I have used a variable twice and that is what is causing this but I am not catching it. I am confident that it is in the function "add_after" as this is the function called to add after a word. I have hand traced a couple of times but with no avail. Could somone please point out my logic error. <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); system("pause"); } //***************************************************** /* 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 ->ptr_to_next_node != NULL) { if(strcmp(word_after,currNode->word)) { assign_new_node(newNode); nextNode = currNode ->ptr_to_next_node; currNode ->ptr_to_next_node = newNode; newNode ->ptr_to_next_node = nextNode; strcpy (newNode ->word,a_word); break; } else currNode = currNode -> ptr_to_next_node; } } </code> |
From: Per W. <pw...@ia...> - 2011-04-26 20:14:36
|
Don't you have access to a debugger? With a debugger, you can single-step the code and see yourself where the program does something that you didn't expect. You seem to have fixed the error where you used one of the parameters to the insert_after() both to locate where to insert and as the new string to insert. When your code "always" (it doesn't really) inserts the word directly after the first word in the list (which isn't true really) then it must be quite reasonable to suspect the code that performs the string comparison. However, you haven't read the manual for strcmp(). if(strcmp(word_after,currNode->word)) strcmp will return three different alternatives: less than zero equal to zero greater than zero Quite logical since the two strings you send in as parameters may be smaller than, equal to, or larger than each other. Your goal is to find a match, i.e. equal. If you google "man strcmp" you would notice that strcmp() returns zero when the two strings are matching. Your if statement on the other hand gets activated if strcmp() returns non-zero, i.e. that the word is greater than or smaller than the word you are looking for. But a more interesting question here - besides your incorrect use of the result from strcmp() is why you mix C and C++ like you do. You are using C++ constructs to read input and print output. But you are not using the C++ string data type for text strings. Instead, you are using the character arrays available in C. What happens if you read in a longer word than your fixed-size character arrays can store? Your previous code used strncpy() - but that one will not zero-terminate strings that are too big to fit. Now you have strcpy() that don't care at all about available space. And the input from cin will not know how many characters that fits. /pwm On Tue, 26 Apr 2011, Jordan Burnam wrote: > Hi everyone, > > I have the following program that I have created and am trying > to get this work correctly. The purpose of this program is to make a > litst from the users input then allow the user to add a word after a > word in the list. So far this list only adds the word after the first > word each time. I have looked at it and ad peers online to view it and > have advised me that I have used a variable twice and that is what is > causing this but I am not catching it. I am confident that it is in the > function "add_after" as this is the function called to add after a word. > I have hand traced a couple of times but with no avail. Could somone > please point out my logic error. > > <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); > system("pause"); > ã > ã > ã > } > //***************************************************** > /* 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 ->ptr_to_next_node != NULL) > { > if(strcmp(word_after,currNode->word)) > { > assign_new_node(newNode); > nextNode = currNode ->ptr_to_next_node; > currNode ->ptr_to_next_node = newNode; > newNode ->ptr_to_next_node = nextNode; > strcpy (newNode ->word,a_word); > break; > } > else > currNode = currNode -> ptr_to_next_node; > } > } > </code> |
From: Michal M. <mol...@se...> - 2011-04-26 20:19:44
|
if(strcmp(word_after,currNode->word)) should be if(strcmp(word_after,currNode->word) == 0) |