Hello there...I need some help when I'm trying to pop data (numbers) from an array into another one (push them). I managed perfectly to input data but I'm completly lost when trying to pop from one array and push it into another one, I tried many things but none of them worked...I'm not sure If I'm clear whith my question :S any help/advices would be helpful.
Here is my the code:

public class PushAndPopStacks implements Stack {
  protected int capacity;
  public static final int CAPACITY = 2;
  protected Object S[];
  protected Object Inv[];
  protected int top = -1;
  public PushAndPopStacks() {
    this(CAPACITY);
  }
  public PushAndPopStacks(int cap) {
    capacity = cap;
    S = new Object[capacity]; 
  }
  public int size() { 
    return (top + 1);
  }
  public boolean isEmpty() {
    return (top == -1);
  }
  public void push(Object element) throws FullStackException {
    if (size() == capacity) {
      doubleArray();
    }
    S[++top] = element;
  }
  public Object top() throws EmptyStackException {
    if (isEmpty())
      throw new EmptyStackException("Stack is empty.");
    return S[top];
    }
  public Object pop() throws EmptyStackException {
    Object element;
    if (isEmpty())
      throw new EmptyStackException("Stack is empty.");
    element = S[top];
    S[top--] = null;
    return element;
    }

    private void doubleArray( ) {
      Object [ ] newArray;

      System.out.println("Stack is full (max size was "+capacity+"). Increasing to "+(2*capacity));
      //double variable capacity
      capacity = 2*capacity;
      newArray = new Object[ capacity ];
      for( int i = 0; i < S.length; i++ )
          newArray[ i ] = S[ i ];
      S = newArray;
    }
    public void inverse(){
        Inv=S;
    }
    public static void main(String[] args) {
        Stack S = new PushAndPopStacks();
        S.push("1");
        S.push("2");
        S.push("3");
      while (!S.isEmpty()) {
        System.out.println(S.pop());
        }        
    }
}

Recommended Answers

All 3 Replies

What errors do you get? What is the output and why you thing it is wrong. Also I don't see where is the method for putting the data from one array to the other.
Maybe you should loop one array and put its values to another, but the 0 index should be put in the length-1, the i index in the length-1-i index

I tried many things and also have many errors so I just tried to paste here a clean code...
I just want to pop Data from the Array S and push it into Array Inv...

Then you need 2 stacks not one. You use a private array in the Stack class. You can create as many instance of the PushAndPopStacks class.
Create 2 PushAndPopStacks instances. You already have one:
Stack S = new PushAndPopStacks()

The PushAndPopStacks should have only one private array. Create 2 PushAndPopStacks. Then call pop method of one instance and push the result into the other.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.