View Javadoc

    1   package baseCode.dataStructure;
    2
    3   /***
    4    * <p>
    5    * Simple Stack implementation
    6    * </p>
    7    * <p>
    8    * Copyright (c) 2004
    9    * </p>
    10   * <p>
    11   * Institution: Columbia University
    12   * </p>
    13   *
    14   * @author Paul Pavlidis
    15   * @version $Id: Stack.java,v 1.5 2004/07/29 08:38:49 pavlidis Exp $
    16   * @deprecated -- use java.util.List instead.
    17   */
    18  public class Stack {
    19
    20     private Object[] stack;
    21     private int top;
    22     private final static int DEFAULTCAPACITY = 10000;
    23
    24     /***
    25      * Build a stack with the default capacity.
    26      */
    27     public Stack() {
    28        this( DEFAULTCAPACITY );
    29     }
    30
    31     /***
    32      * Build a stack with a given capacity.
    33      *
    34      * @param capacity int
    35      */
    36     public Stack( int capacity ) {
    37        stack = new Object[capacity];
    38     }
    39
    40     /***
    41      * Remove the most recently added item.
    42      *
    43      * @return Object
    44      */
    45     public Object pop() {
    46        if ( isEmpty() ) {
    47           return null;
    48        }
    49        Object topObj = top();
    50        stack[top--] = null;
    51        return topObj;
    52
    53     }
    54
    55     /***
    56      * Add an item to the stack.
    57      *
    58      * @param obj Object
    59      */
    60     public void push( Object obj ) {
    61        if ( isFull() ) {
    62           throw new IndexOutOfBoundsException( "Stack overflow" );
    63        }
    64        stack[++top] = obj;
    65     }
    66
    67     /***
    68      * Get the most recently added item, without removing it.
    69      *
    70      * @return Object
    71      */
    72     public Object top() {
    73        if ( isEmpty() ) {
    74           return null;
    75        }
    76        return stack[top];
    77     }
    78
    79     /***
    80      * @return boolean
    81      */
    82     public boolean isEmpty() {
    83        return top == -1;
    84     }
    85
    86     /***
    87      * @return boolean
    88      */
    89     public boolean isFull() {
    90        return top == stack.length - 1;
    91     }
    92
    93  }