Skip to content
Advertisement

arrayImpOfStack.java how to reverse number in output?

I need to write arrayImpOfStack.java then write a main method to read a sequence of numbers and using the stack operation print them in reverse order

How to reverse number in output ?

I have 2 class

class 1

class stackAr   
{  
 int elements [];       
 int top; // is the index of the cell containing the last elements added to the stack.
  
 stackAr(int maxlength) {top = maxlength; elements = new int[maxlength];  };

// initially the value of the variable top = maxlength which means the stack is empty
// So the variable top must be decremented before pushing new element, which means 
// the first element is pushed at cell numbered maxlength-1
// the second element is pushed at the cell numbered maxlength-2
// the third element is pushed at the cell numbered maxlength-3
// an so on the stack is full when top = 0

void push(int x)
{ if (top == 0) System.out.println("the Stack is Full  ");
  else elements[--top] = x;
}

Boolean isEmpty() // Note the stack is empty when top = elements.length which is the maxlength
{
if (top == elements.length) return true; else return false;
}

void pop()   // pop increment the variable top to ignore the last element added to the stack
{
if (!isEmpty()) top++ ;
   else System.out.println("Stack is Empty  ");
}

int Top() // return  the last element added to the stack
{if (!isEmpty()) return elements[top];
     else  {System.out.println("Stack is Empty  "); return top;}
}      
void MakeNull()   {top = elements.length;}  // make the stack empty
} 

and i have class main

public static void main(String[] args) {
stackAr s = new stackAr(20);
s.push(1);
s.push(9);
s.push(2);
s.push(10);
 
while(!s.isEmpty())
  { 
 System.out.println(s.Top());
   s.pop();
  }

Advertisement

Answer

You can do it recursively:

public static void printStackInReverse(stackAr s){
    if(!s.isEmpty()){
        int e = s.Top();
        s.pop();
        printStackInReverse(s);
        System.out.println(e);
        s.push(e);
    }
}

In your case:

public static void main(String[] args) {
  stackAr s = new stackAr(20);
  s.push(1);
  s.push(9);
  s.push(2);
  s.push(10);
  printStackInReverse(s);
}

Full Example:

class stackAr
{
    int elements [];
    int top; // is the index of the cell containing the last elements added to the stack.

    stackAr(int maxlength) {top = maxlength; elements = new int[maxlength];  };

    void push(int x)
    { if (top == 0) System.out.println("the Stack is Full  ");
    else elements[--top] = x;
    }

    Boolean isEmpty() // Note the stack is empty when top = elements.length which is the maxlength
    {
        return top == elements.length;
    }

    void pop()   // pop increment the variable top to ignore the last element added to the stack
    {
        if (!isEmpty()) top++ ;
        else System.out.println("Stack is Empty  ");
    }

    int Top() // return  the last element added to the stack
    {if (!isEmpty()) return elements[top];
    else  {System.out.println("Stack is Empty  "); return top;}
    }
    void MakeNull()   {top = elements.length;}  // make the stack empty

    public static void printStackInReverse(stackAr s){
        if(!s.isEmpty()){
            int e = s.Top();
            s.pop();
            printStackInReverse(s);
            System.out.println(e);
            s.push(e);
        }
    }


    public static void main(String[] args) {
        stackAr s = new stackAr(20);
        s.push(1);
        s.push(9);
        s.push(2);
        s.push(10);
        printStackInReverse(s);
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement