structure
Class StackList<ELTTYPE>

java.lang.Object
  extended by structure.AbstractStructure<ELTTYPE>
      extended by structure.AbstractLinear<ELTTYPE>
          extended by structure.AbstractStack<ELTTYPE>
              extended by structure.StackList<ELTTYPE>
All Implemented Interfaces:
java.lang.Iterable<ELTTYPE>, Linear<ELTTYPE>, Stack<ELTTYPE>, Structure<ELTTYPE>

public class StackList<ELTTYPE>
extends AbstractStack<ELTTYPE>
implements Stack<ELTTYPE>

An implementation of a stack, based on lists. The head of the stack is stored at the head of the list, allowing the stack to grow and shrink in constant time. This stack implementation is ideal for applications that require a dynamically resizable stack 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){
           StackList reverseStack = new StackList();
           String s = arguments[0];
            
           for(int i=0; i < s.length(); i++){
               reverseStack.push(new Character(s.charAt(i)));
           }

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

           System.out.println();
     }
 }
 

See Also:
Stack, StackVector, StackArray, AbstractStack

Constructor Summary
StackList()
          Construct an empty stack.
 
Method Summary
 void add(ELTTYPE value)
          Add a value to the top of the stack.
 void clear()
          Remove all elements from the stack.
 boolean empty()
          Determine if the stack is empty.
 ELTTYPE get()
          Get a reference to the top value in the stack.
 java.util.Iterator<ELTTYPE> iterator()
          Returns an iterator for traversing the structure.
 ELTTYPE 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 structure.AbstractStack
getFirst, peek, pop, push
 
Methods inherited from class structure.AbstractLinear
remove
 
Methods inherited from class structure.AbstractStructure
contains, elements, hashCode, isEmpty, values
 
Methods inherited from class java.lang.Object
equals, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface structure.Stack
getFirst, peek, pop, push
 
Methods inherited from interface structure.Structure
contains, elements, isEmpty, remove, values
 

Constructor Detail

StackList

public StackList()
Construct an empty stack.

Post:
constructs a new stack, based on lists
Method Detail

clear

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

Specified by:
clear in interface Structure<ELTTYPE>
Post:
removes all elements from stack

empty

public boolean empty()
Determine if the stack is empty. Provided for compatibility with java.util.Stack.empty.

Specified by:
empty in interface Linear<ELTTYPE>
Specified by:
empty in interface Stack<ELTTYPE>
Overrides:
empty in class AbstractLinear<ELTTYPE>
Returns:
True iff the stack is empty.
See Also:
AbstractStructure.isEmpty()
Post:
returns true iff the stack is empty

iterator

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

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

get

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

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

add

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

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

remove

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

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

size

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

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

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.
Post:
returns a string representation of stack