//
//  VectorStack.java
//  
//
//  Created by Herbert J. Bernstein on 2/24/11.
//  Copyright 2011 All rights reserved.
//

public class VectorStack {
    int capacity;
    int values[];
    int top;
    
    VectorStack() {
        capacity = 1;
        values = new int[1];
        top = 0;
    }
    
 
    // A copy constructor, but only for the top level,
    // The rest of the stacks are merged, but because
    // We only work directly with the front, this works.
    VectorStack(VectorStack l) {
        for (int ttop = 0; ttop < l.top; ttop++) {
            this.push(l.values[ttop]);
        }
    }
    
    // Push a new value onto the stack;

    int push ( int value ) {
        if (top >= capacity) {
            int newval[] = new int[(capacity<=0)?2:capacity*2];
            for (int ii=0; ii< top; ii++) newval[ii] = values[ii];
            values=newval;
            capacity = newval.length;
        }
        values[top++] = value;
        return 0;
    }
        
    // pop data from front of the stack
    
    int pop ( ) {
        int retvalue = 0;
        if (top > 0) {
            retvalue = values[--top];
        }
        if (top == 0) {
            capacity = 1;
            values = new int[1];
            top = 0;            
        }
        return retvalue;
    }
    
    // test for empty
    
    boolean empty( ) {
        return (top==0);
    }
    
    //print list stack
    
    int print( ) {
        for (int ptr = top-1; ptr >= 0; ptr--) {
            System.out.println(values[ptr]);
        }
        return 0;
    }
    
}