structure5
Class StackArray<E>

java.lang.Object
  extended by structure5.AbstractStructure<E>
      extended by structure5.AbstractLinear<E>
          extended by structure5.AbstractStack<E>
              extended by structure5.StackArray<E>
All Implemented Interfaces:
java.lang.Iterable<E>, Linear<E>, Stack<E>, Structure<E>

public class StackArray<E>
extends AbstractStack<E>
implements Stack<E>

An implementation of a stack, based on array. The head of the stack is stored in the first position of the array, allowing the stack to grow and shrink in constant time. This stack implementation is ideal for applications that require a stack with a known maximum size that expands in constant time.

Example usage:

To reverse a string, we would use the following:

 public static void main(String[] arguments)
 {
     if(arguments.length > 0){
         StackArray reverseStack = new StackArray(arguments[0].length());
         String s = arguments[0];
          
         for(int i=0; i < s.length(); i++){
             reverseStack.push(new Character(s.charAt(i)));
         }

         while(!reverseStack.AbstractLinear.empty()){
             System.out.print(reverseStack.AbstractStack.pop());
         }

         System.out.println();
     }
 }
 

See Also:
Stack, StackVector, StackList, AbstractStack

Field Summary
protected  java.lang.Object[] data
          The array of value references.
protected  int top
          An index to the top element of the stack.
 
Constructor Summary
StackArray(int size)
          Construct a stack capable of holding at least size elements.
 
Method Summary
 void add(E item)
          Add a value to the top of the stack.
 void clear()
          Remove all elements from the stack.
 E get()
          Get a reference to the top value in the stack.
 boolean isEmpty()
          Determine if the stack is empty.
 boolean isFull()
          Determine if the stack is full.
 java.util.Iterator<E> iterator()
          Returns an iterator for traversing the structure.
 E remove()
          Remove a value from the top of the stack.
 int size()
          Determine the number of elements in the stack.
 java.lang.String toString()
          Construct a string representation of the stack.
 
Methods inherited from class structure5.AbstractStack
getFirst, peek, pop, push
 
Methods inherited from class structure5.AbstractLinear
empty, remove
 
Methods inherited from class structure5.AbstractStructure
contains, elements, hashCode, values
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface structure5.Stack
empty, getFirst, peek, pop, push
 
Methods inherited from interface structure5.Structure
contains, elements, remove, values
 

Field Detail

top

protected int top
An index to the top element of the stack.


data

protected java.lang.Object[] data
The array of value references. Top of the stack is higher in array.

Constructor Detail

StackArray

public StackArray(int size)
Construct a stack capable of holding at least size elements.

Parameters:
size - The maximum size of the stack.
Postcondition:
an empty stack with initial capacity of size is created
Method Detail

clear

public void clear()
Remove all elements from the stack.

Specified by:
clear in interface Structure<E>
Postcondition:
removes all elements from stack

add

public void add(E item)
Add a value to the top of the stack.

Specified by:
add in interface Linear<E>
Specified by:
add in interface Stack<E>
Specified by:
add in interface Structure<E>
Parameters:
item - The value to be added.
See Also:
AbstractStack.push(E)
Postcondition:
adds an element to stack; Will be next element popped if no intervening push

remove

public E remove()
Remove a value from the top of the stack.

Specified by:
remove in interface Linear<E>
Specified by:
remove in interface Stack<E>
Returns:
The value removed from the top of the stack.
See Also:
AbstractStack.pop()
Precondition:
stack is not empty
Postcondition:
removes and returns the top element from stack;

get

public E get()
Get a reference to the top value in the stack.

Specified by:
get in interface Linear<E>
Specified by:
get in interface Stack<E>
Returns:
A reference to the top element of the top of the stack.
Precondition:
stack is not empty
Postcondition:
returns the top element (most recently pushed) from stack

iterator

public java.util.Iterator<E> iterator()
Description copied from interface: Structure
Returns an iterator for traversing the structure.

Specified by:
iterator in interface java.lang.Iterable<E>
Specified by:
iterator in interface Structure<E>
Returns:
an iterator for traversing the structure
See Also:
AbstractIterator, Iterator, Enumeration, Structure.elements()

size

public int size()
Determine the number of elements in the stack.

Specified by:
size in interface Linear<E>
Specified by:
size in interface Stack<E>
Specified by:
size in interface Structure<E>
Returns:
The number of values within the stack.
Postcondition:
returns the size of the stack

isEmpty

public boolean isEmpty()
Determine if the stack is empty.

Specified by:
isEmpty in interface Structure<E>
Overrides:
isEmpty in class AbstractStructure<E>
Returns:
True iff the stack is empty.
See Also:
AbstractLinear.empty()
Postcondition:
returns true iff the stack is empty

isFull

public boolean isFull()
Determine if the stack is full.

Returns:
True iff there is no more room in the stack.
Postcondition:
returns true iff the stack is empty

toString

public java.lang.String toString()
Construct a string representation of the stack.

Overrides:
toString in class java.lang.Object
Returns:
A string representing the stack.
Postcondition:
returns a string representation of stack