I have been trying to write a recursive function that searches a stack, but leaves the stack in its original state. I may pus h and pop the stack, but not use a helper stack or any other data stucture.
And yes, this is homework, so I do not expect a complete coded answer :). A little help about how to approach the stack so that after the recursive search is finished the stack is intact would be appreciated.
The recursive function that searches the stack for a specified item(but destroys the stack) is given below:
template <class Type>
Type getNth(stack(Type) & s, int n)
{
if(s.empty())
return -1;
if(s.top() == n)
return s.top();
if(s.top() != n && s.empty())
return -1;
else
s.pop();
return getNth(s, n);
}
This works, so far. Any help greatly appreciated
you should store the pop()
ed value, and the recursive call result, and push()
the pop()
ed value back, before returning.
your else should look something like that: [other than it, it looks fine]
else
temp = s.pop();
retVal = getNth(s, n);
s.push(temp);
return retVal;
(*)forgive me for not declaring temp
and retVal
, You can understand the general idea from this..
EDIT:
I decided to add a simple example what is happening, assume your stack is
|1|
|2|
|3|
|4|
---
and you are call getNth(s,3): this what will happen to the stack
after 1st pop() and getNth(): [stop condition was not reached, so keep going]
|2|
|3|
|4|
---
2nd pop(),getNth(): [again, keep going]
|3|
|4|
---
now, when you check if s.top() == n, you realize they are! so you return n.
when coming back from the recursion, s.push(temp)
is called, and temp==2
, so we get:
|2|
|3|
|4|
---
and we return retVal again, now back from the recursion, we use s.push()
again, and we get:
|1|
|2|
|3|
|4|
---
the original stack! and return the same returnVal, that was returned by the recursion!
NOTE: This is not your question, but the name of the function implies you don't want to return the value you were searching for, but rather the nth element in the stack, meaning, if your stack is:
|5|
|8|
|8|
|8|
|2|
|4|
---
getNth(2)
will need to return 8, and NOT 2, as your question describes.
But I cannot possibly know that for sure, and if it is the case, I think you have enough tools to handle this question without too much problems!
good luck!
EDIT 2:
after the discussion in the comments, it is clear that the OP wanted something a bit different then what the original question describes, so therefore the extra edit:
Your solution is searching for an element and returns it, probably what you want to do is COUNT until these element, and then return, should be something like that [again, not declaring all variables, it won't compile, it's just a direction]:
template <class Type>
Type getNth(stack(Type) & s, int n)
{
if(s.empty()) {return -1; } //note that in C++ throwing an exception here will be more wise, since -1 might be not matching to Type
else if(n == 0) { return s.top(); }
else {
temp = s.pop();
retVal = getNth(s, n-1);
s.push(temp);
return retVal;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With