I need to create a program where the user is asked to enter two chars which will represent a starting node and and ending node, the program is then supposed to go from start to finish and return the path it took. How do I tell the program that the starting node should be the one that the user entered?
string path = "";
bool found_path = false;
for ( /*loop until end of list or found last char*/ )
{
if (!found_path and (value == start or value == last))
{
append value to path string
found_path = true;
}
elseif (found_path)
append current value to the path string
}
return path;
@Smac89: I don't think that OP is asking for the algorithm that handles finding the path, but is instead asking how to ensure that they can get input that represents the nodes so that the algorithm can be called.
@clarkkent: We may need a few more details such as:
1. How are node names/id's represented in your code?
2. Do you have access to a master list of the existing nodes in the graph?
Despite the lack of clarity, here is my high level suggestion.
#include <iostream>
usingnamespace std;
int main( void ){
// I am assuming that you have a predefined map:
NodeMap nMap = mkNodeMap();
// Read the user input:
char start_node_char;
char end_node_char;
cin >> start_node_char;
cin >> end_node_char;
// Search the node map for the start and end nodes:
Node start = nMap.get(start_node_char);
Node end = nMap.get(end_node_char);
// Check to see if both are not null
// If either one is null, then the node does not exist
if( start == NULL || end == NULL ){
return EXIT_FAILURE;
}else{ // search for node:
nMap.getPath(start,end); // Call your search function
}
}
Thanks for your responses. Here is the code I have so far, it is not working at the moment, as it goes into an infinite loop :/
I just dont know how to get the user to dictate where the list starts and ends.
Then set up a switch statement to determine what to set ptr and ptr2 to.
As for your looping problem, your program is working fine. Feel free to look at this version I modified so you can see what's going on (no user input):
Thanks for your response, I have a couple of questions...what do I do with the starting and destination variables, how do I make a pointer point to them so they can be the start and end of the list? Ive tried something like
ListNode *ptr = starting; //but since they are different types I get an error
And why does the loop not stop when ptr and ptr2 are the same? could it be that once inside the loop all the if statements have to happen and the program does not know when they are equal since by the end of each loop they are not equal anymore?
Thanks again to all who have tried to help, i really appreciate it.
Not sure how the program should be set up but right now the reason you're stuck in an infinite loop isn't that ptr equals B and is then changed before the next loop iteration (though this is also a possible problem which needs to be fixed). The reason is that ptr goes through the following repeating cycle and is never equal to B:
F to C to F to E to F, then next while() iteration
F to C to F to E to F, then next while() iteration
and so on.
Maybe you didn't copy down A-I correctly (assuming they're copied), maybe your if tests should be a series of if-else tests, maybe something else. Sadly there's no way for us to know for certain.