ca.bcit.cst.comp2526.assign2b.solution
Class Stack

java.lang.Object
  extended by ca.bcit.cst.comp2526.assign2b.solution.Stack

public class Stack
extends java.lang.Object

A Last In First Out (LIFO) data structure that holds integers. This implementation is a fixed sizes stack.

See Also:
Wikipedia

Field Summary
static int MIN_SIZE
          The smallest possible stack size.
 
Constructor Summary
Stack(int size)
          Construct an empty stack of the specified size.
 
Method Summary
 int pop()
          Removes the top item from the stack.
 void push(int val)
          Push a value onto the top of the stack.
 int size()
          Get the total number of values the stack can hold.
 int topValue()
          Return the top value off of the stack withour actually removing it.
 int unused()
          Get the number of values that can still be pushed onto the stack.
 int used()
          Get the number of values that are currently on ths stack.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIN_SIZE

public static final int MIN_SIZE
The smallest possible stack size.

Constructor Detail

Stack

public Stack(int size)
      throws java.lang.IllegalArgumentException
Construct an empty stack of the specified size.

Parameters:
size - the maximum size of the stack.
Throws:
java.lang.IllegalArgumentException - if the size < 1
See Also:
MIN_SIZE
Method Detail

size

public int size()
Get the total number of values the stack can hold.

Returns:
the total number of values that the stack can hold.

used

public int used()
Get the number of values that are currently on ths stack.

Returns:
the number of values that are currently on the stack.

unused

public int unused()
Get the number of values that can still be pushed onto the stack.

Returns:
the number of values that can still be pushed onto the stack.

push

public void push(int val)
          throws StackOverflowException
Push a value onto the top of the stack. The next call to pop will remove this item. The next call to topValue will return this item.

Parameters:
val - the value to push onto the stack.
Throws:
StackOverflowException - if the unused portion of ths tack is 0.
See Also:
pop(), topValue(), unused()

pop

public int pop()
        throws StackUnderflowException
Removes the top item from the stack. The top item is is one that was last pushed onto the stack.

Returns:
the value on the top of the stack.
Throws:
StackUnderflowException - if there are no values on the stack.
See Also:
push(int)

topValue

public int topValue()
             throws StackUnderflowException
Return the top value off of the stack withour actually removing it. The top item is is one that was last pushed onto the stack.

Returns:
the top value off of the stack.
Throws:
StackUnderflowException - if there are no values on the stack.
See Also:
push(int)