[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> |