com.mhhe.clrs2e
Class StackArray

java.lang.Object
  |
  +--com.mhhe.clrs2e.StackArray
All Implemented Interfaces:
Stack

public class StackArray
extends java.lang.Object
implements Stack

Implements an elementary stack from page 201 of Introduction to Algorithms, Second edition.


Field Summary
protected static int EMPTY
          The index of the top when thestack is empty.
protected  java.lang.Object[] stack
          The array implementing the stack.
protected  int top
          The index of the top of the stack.
 
Constructor Summary
StackArray()
          Makes an empty stack with 1 slot.
StackArray(int size)
          Makes an empty stack with a given number of slots.
 
Method Summary
 boolean isEmpty()
          Returns true if the stack is empty, false otherwise.
 java.lang.Object pop()
          Pops an object from the stack, returning the popped object.
 void push(java.lang.Object x)
          Pushes an object onto the stack.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

top

protected int top
The index of the top of the stack.


stack

protected java.lang.Object[] stack
The array implementing the stack.


EMPTY

protected static final int EMPTY
The index of the top when thestack is empty.

See Also:
Constant Field Values
Constructor Detail

StackArray

public StackArray()
Makes an empty stack with 1 slot.


StackArray

public StackArray(int size)
Makes an empty stack with a given number of slots.

Parameters:
size - The number of slots.
Method Detail

isEmpty

public boolean isEmpty()
Returns true if the stack is empty, false otherwise.

Specified by:
isEmpty in interface Stack

push

public void push(java.lang.Object x)
Pushes an object onto the stack. If the stack is full, first doubles its size.

Specified by:
push in interface Stack
Parameters:
x - Object to be pushed.

pop

public java.lang.Object pop()
Pops an object from the stack, returning the popped object.

Specified by:
pop in interface Stack
Throws:
StackUnderflowException - if the stack was already empty.