Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing stack without popping elements java

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

like image 259
user2270715 Avatar asked Dec 02 '25 06:12

user2270715


2 Answers

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.

like image 197
Aaron Digulla Avatar answered Dec 07 '25 05:12

Aaron Digulla


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

System.out.println(Arrays.toString(myStack.toArray()));
like image 36
Mangesh Avatar answered Dec 07 '25 05:12

Mangesh