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