printing stack without popping elements java

34,415

Solution 1

If you use the built-in java.util.Stack type, then this derives from Vector, so you can use getElement(int) to read elements at any stack depth.

If this is your own code, you will have to add a method to do the same.

Alternatively, you can pop the elements into another stack or a List type and then rebuild the stack after printing but that would be very inefficient and your teacher will most probably frown about such a solution.

Solution 2

There is a simple workaround if you just want to see the contents without any fancy stuff.

System.out.println(Arrays.toString(myStack.toArray()));

Solution 3

if (!_stack.empty())

Check whether the Stack is Empty

for(int i=_stack.size()-1; i>=0;i--)  
System.out.println(_stack.get(i));

getting stack values

Share:
34,415

Related videos on Youtube

user2270715
Author by

user2270715

Updated on August 25, 2020

Comments

  • user2270715
    user2270715 over 3 years

    for a task I have to write a method which prints the stack, that part is easy

    public void print(stack s)
    {
       while(!isEmpty())
       {
          System.out.println(s.peek());
          s.pop();
       }
    
    }
    

    The problem is that after I printed the stack, my task is to print the bottom element on the stack, which isn't there any more cause I used s.pop() in my print method. This is my code for printing the bottom element.

    public void bottom(stack s)
    {
      if(isEmpty())
       {
         System.out.println("Stack is empty");
       }
      else
       {
         System.out.println(stackArray[0]);
       }
    }
    

    My question is: How should i modifie the print method so I don't have to pop the elements from the stack? Or is there another way to make it so that the stack still holds my elements after using print method?

    as resquested this is the stack we're using in our classes(most of it is in dutch):

    public class MyStack
    {
        protected Object[ ] stackArray;
        protected int top;
        private int grootte;
        private static final int DEFAULT_GROOTTE = 10;
    
        public MyStack( )
        {
            grootte = DEFAULT_GROOTTE;
            stackArray = new Object[grootte];
            top = 0;
        }
    
        public boolean isEmpty( )
        {
            if (top == 0)
                    return true;
                else 
                    return false;
        }
    
        public void push(Object e)
        {
            if (top == grootte)
                allocateMore( );
            stackArray[top] = e;
            top++;
        }
    
        public Object pop( )
        {
                if(isEmpty( ))
                {
                    System.out.println("Stack leeg : er kan geen element van de stack afgehaald worden.");
                    return null;
                }
                        top--;
                        return stackArray[top];
    
    }
        public Object peek( )
        {
                if(isEmpty( ))
                {
                    System.out.println("Stack leeg : er kan geen topelement van de stack getoond worden.");
                    return null;
                }
                        return stackArray[top-1];
        }
    
        public int size( )
        {
            return top;
        }
    
        private void allocateMore( )
        {
            Object[ ] original = stackArray;
            grootte = grootte * 2;
            stackArray = new Object[ grootte];
            for(int i = 0; i < grootte/2; i++)
            {
                stackArray[i] = original[i];
        }
        }
    
    }
    

    since my rep isn't high enough to answer my own question a quick edit

    I think I've found an other way to print the stack using this

    public void print(stack s)
    {
     for(int i =top-1; i>=0;i--)
       System.out.println(stackArray[i]);
    }
    

    it probably isn't the best way to do it, but it's working :P

    • Vitaly
      Vitaly about 11 years
      Could you add your Stack class?
    • Madhu Bose
      Madhu Bose almost 7 years
      Using Iterator : public void printStack(Stack stack){ Iterator<> itr = stack.iterator(); while(itr.hasNext()){ sysout(itr.next); } }