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());
}
}
}