1

Please note that it is just reverse printing not reversing a stack

What I want to do is with the help of recursion print the stack i.e bottom to top printing.

I have tried myself but the result is not what I expected.My code looks like this.

#include <bits/stdc++.h>
using namespace std;


void print(stack<char>st){
   if(st.empty()){return;}
   st.pop();
   print(st);
   cout<<st.top();
}

 int main() {
    // Assume that I have stack already made....
    print(st);
    cout<<endl;
 }
 return 0;
}

Would anybody mind having pointing out my mistake ? Also when I pass stack by reference the results are unexpected.Thanks for support.

14
  • What is the result and why isn't it what you expected? Commented May 29, 2016 at 8:06
  • Runtime error is what it is showing me.In the above implementation.Thanks for looking :) @fractalwrench Commented May 29, 2016 at 8:08
  • Is my algorithm right to print reverse stack ? Commented May 29, 2016 at 8:11
  • stack<T> is designed to hide its implementation. You'd have to write your own stack based on a vector<T>, say. You're getting an error because when you pop the last element, the stack is empty, then you're trying to print st.top() which no longer exists. Move the cout to before the pop. Commented May 29, 2016 at 8:13
  • Your current implementation prints top to bottom, and stack<T> won't support what you want. You have to write your own MyStack class. Also, by calling pop you're removing the top of the stack - you may just want to print the stack without emptying it. Commented May 29, 2016 at 8:15

2 Answers 2

3

Why don't you store the st.top() at variable and print it later.

void print(stack<char>st)
{
    if(st.empty())
    {
        return;
    }

    char top = st.top();
    st.pop();
    print(st);

    cout<<top<<endl;
}

Let me explain you:-

Suppose, Your stack --> 0, 1

Here, Call Hierarchy

  1. print({0, 1}) { // stack after pop -- {0} print({0}) }
  2. print({0}) { // stack after pop -- {} print ({}) // here you want to print top of empty stack // which gives the exception }
Sign up to request clarification or add additional context in comments.

5 Comments

Why have you stored top ?
@behnc because if you don't it is lost in the ether after the pop before the recursion (which is precisely the problem in your code).
@behnc, see the example at the answer
Now I got it sir.Consider reverse printing of linked list.we need not to store any data in case of linked list because we are not deleting anything.But in case of stack we do delete by pop.So before we delete we store.Am I right sir ? @WhozCraig
@behnc pop does exactly what its name implies: pops the top element from the stack. If it was the last element, then the stack is then empty. Afterward, invoking top() on an empty stack (as your code does) rears your exception. Mahedi's description is both accurate and worth of answer-selection. It describes in-detail what is going wrong.
0

pop is function get element to print like reverse

 void printStack()
{
   if(!isEmpty())
   {
     int temp = pop();
     printStack();
     printf(" %d ", temp);
     push( temp);
   }
}

int pop() 
{
  if (isEmpty())
    printf("Stack is Empty...\n");
  else {
      st.top = st.top - 1;
      return st.array[st.top+1];
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.