Class ExpressionElementStack

java.lang.Object
  |
  +--java.util.AbstractCollection
        |
        +--java.util.AbstractList
              |
              +--java.util.Vector
                    |
                    +--java.util.Stack
                          |
                          +--ExpressionElementStack

public class ExpressionElementStack
extends java.util.Stack

This is a simple extension of Stack which is designed specifically to hold ExpressionElements. The major additions are nextElement() which returns an ExpressionElement as opposed the Object that is usually returned. The same goes for top() which adds the same typecasting for Stack.peek().

Other functionality added was the capacity to reverse() the elements in the stack and to dump() the information in the stack to System#out.

See Also:
Serialized Form

Fields inherited from class java.util.Vector
capacityIncrement, elementCount, elementData
 
Fields inherited from class java.util.AbstractList
modCount
 
Constructor Summary
ExpressionElementStack()
           
 
Method Summary
 void dump()
          Prints the elements of the stack to java.lang.System#out.
 ExpressionElement nextElement()
          Removes the top element from the stack and returns it
 void reverse()
          Reverses the order of the elements in the stack.
 ExpressionElement top()
          Check the top element from the stack and returns it; the top remains on the stack.
 
Methods inherited from class java.util.Stack
empty, peek, pop, push, search
 
Methods inherited from class java.util.Vector
add, add, addAll, addAll, addElement, capacity, clear, clone, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, lastElement, lastIndexOf, lastIndexOf, remove, remove, removeAll, removeAllElements, removeElement, removeElementAt, removeRange, retainAll, set, setElementAt, setSize, size, subList, toArray, toArray, toString, trimToSize
 
Methods inherited from class java.util.AbstractList
iterator, listIterator, listIterator
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ExpressionElementStack

public ExpressionElementStack()
Method Detail

nextElement

public ExpressionElement nextElement()
Removes the top element from the stack and returns it
Returns:
the top of the stack

top

public ExpressionElement top()
Check the top element from the stack and returns it; the top remains on the stack.
Returns:
the top of the stack

reverse

public void reverse()
Reverses the order of the elements in the stack. The algorithm used is this:
  • n = the size of the stack
  • i = 1
  • while i < n - i
    • swap elements i and n - i
    • i = i + 1

This method is useful because sometimes when a stack is being created it ends up being created in the reverse order than what it needs to be in to be useful. (Like ArithmeticEquation.ArithmeticEquation(java.lang.String) for example.) A stack is a First In Last Out structure and what is needed sometimes is a Last In Last Out structure. In order to get one I could either create a new queue type or add this method to stack. This was by far the more efficient solution.

dump

public void dump()
Prints the elements of the stack to java.lang.System#out. Each element is on its own line and includes the ExpressionElement.typeString() and a string representation of the element.

This function is useful for debugging.