Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6389395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:23:53+00:00 2026-05-25T03:23:53+00:00

I have been trying to write a recursive function that searches a stack, but

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:23:54+00:00Added an answer on May 25, 2026 at 3:23 am

    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;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to write a recursive version of function itoa , the
I have been trying to write a constructor function that would make an object
I have been trying to write a small app with its own option windows.
I have been trying to write an image on a layer using Quartz but
I have been trying to write a regex that will remove whitespace following a
I am new to programming. I have been trying to write a function in
I have been trying to write a set of htaccess rules that will do
I have been trying to write an app that periodically parses the contents of
I have a WPF Application that I have been trying to write in the
I have been trying to write some applescript that checks to see if the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.