Reputation: 159
Here is an outline of the code containing the relevant part of my code.
Inside the empprint function i call a bfs print function, which calls itself recursively till its done printing everything that needs to be printed, after which its supposed to return me back to empprint function. But the return statement in bfsprint doesn't take me back to empprint.
One possible reason I can think of is that bfsprint calls itself recursively so it will only return to the last bfsprint method that called it instead of empprint function but it doesnt seem to solve my problem. I m stuck up with a code whose execution doesnt terminate.
void node::empprint(node* myroot)
{
//do something
bfsprint(c);
cout<<"pt 5"; //this cout is not reached
return;
}
void node::bfsprint(Linklist<node*> noddy)
{
// lot of code to implement breadth-first search. No issue
if(c.getHead()==NULL) cout<<"1" //this does print 1 to output
if(c.getHead()==NULL) return; //I think this should send me back to empprint
// and print "pt 5" on output but program hangs.
// instead of this happening
bfsprint(c);
}
If anybody thinks this might be influenced by other code in the method , I will add it but I dont think its the case.
Upvotes: 0
Views: 87
Reputation: 5882
If your call stack looks like:
node::empprint
node::bfsprint
node::bfsprint
then returning from the final call will result in
node::empprint
node::bfsprint
So your still N calls deep away from getting back to node::empprint.
You could set a bool in the class to return back out, but thats a bit hacky..
void node::bfsprint(Linklist<node*> noddy)
{
if ( something ) { m_unwindstack = true; }
// setting the bool to force returning early/stop recursion once m_unwindstack is true to get back to empprint
if ( m_unwindstack ) { return; }
}
Edit: By the way if you're doing anything with Linklist you'll never seen the changes since your passing a copy of the data. You should pass a reference Linklist&.
Also Linklist seems like your own class? So if you don't use a reference then be sure its copyable otherwise bad things will happen.
Upvotes: 1