Reverse a stack

for(int i=0;i<o.SizeofStack();i++)
{
h.Push(o.Pop());
}
h.Display();
this loop is working only 3 times for 50=>40=>30=>20=>10=>END this input of o.

Hi @Kapsime_S,
This loop will work for sizeofstack/2 times. This is because, every time you pop an element the sizeofstack will decrease by 1 and i is incremented by +1 every time it completes a block of code. You can store the sizeofstack in a variable before the for loop. Your code should work after that.

I tried doing that but the result is still same.
int k=o.SizeofStack(); added this before loop.
recursive method is also not working because of this.

@Kapsime_S can you please share your code as well? Thanks

public class Reverse_Stack {

public static void ReverseStack(usingArrays o,usingArrays h,int idx) throws Exception
    {
        int k=o.SizeofStack();
         for(int i=0;i<o.SizeofStack();i++)
        {
            h.Push(o.Pop());
        }
         h.Display();
        
         //RECURSIVE METHOD

// if(o.isEmpty()==true)
// return;
// o.Display();
// int item=o.Pop();
//
// ReverseStack(o, h, idx+1);
// h.Push(item);
//
// if(idx==0)
// {
// while(!h.isEmpty())
// {
// o.Push(h.Pop());
// }
// }
//
}

public static void main(String[] args) throws Exception {
    
    usingArrays Original =new usingArrays(5);
    usingArrays Helper =new usingArrays(5);
    
    for(int i=0;i<5;i++)
    {
        Original.Push((i+1)*10);
    }
    System.out.print("ORIGINAL STACK: ");
    Original.Display();
    
    System.out.print("AFTER REVERSING:");
    ReverseStack(Original,Helper,0);

       
}

}

Hey @Kapsime_S,
Try to share your complete code also, in the code snippet given below:
int k=o.SizeofStack();
for(int i=0;i<o.SizeofStack();i++)
{
h.Push(o.Pop());
}
you need to write it as i<k instead i<o.SizeofStack(), because to avoid i being compared to a dynamic value we are storing it in variable k.

package stacks;

public class Reverse_Stack {

public static void ReverseStack(usingArrays o,usingArrays h,int idx) throws Exception
    {
        int k=o.SizeofStack();
        
         for(int i=0;i<k;i++)
        {
            h.Push(o.Pop());
        }
         h.Display();
                 
    }
    
public static void main(String[] args) throws Exception {
    
    usingArrays Original =new usingArrays(5);
    usingArrays Helper =new usingArrays(5);
    
    for(int i=0;i<5;i++)
    {
        Original.Push((i+1)*10);
    }
    System.out.print("ORIGINAL STACK: ");
    Original.Display();
    
    System.out.print("AFTER REVERSING:");
    ReverseStack(Original,Helper,0);

       
}

}
this is full code.
and the error msg.
ORIGINAL STACK: 50=>40=>30=>20=>10=>END
Exception in thread “main” java.lang.Exception: STACK IS EMPTY
at stacks.usingArrays.Pop(usingArrays.java:47)
at stacks.Reverse_Stack.ReverseStack(Reverse_Stack.java:12)
at stacks.Reverse_Stack.main(Reverse_Stack.java:31)
AFTER REVERSING:C:\Users\DELL\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

h.Push(o.Pop());
something is wrong in this line. in recursive method also this line is giving error

Hey @Kapsime_S,
There might be some problem with your pop function because this should work. Anyways, implement it using a while loop.
while(o.SizeofStack()!=0)
{
h.Push(o.Pop());
}
Try this approach and see what the answer you are getting.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.