© 1998-2002 McGraw-Hill

structure
Class StackVector

java.lang.Object
  |
  +--structure.AbstractStructure
        |
        +--structure.AbstractLinear
              |
              +--structure.AbstractStack
                    |
                    +--structure.StackVector
All Implemented Interfaces:
Linear, Stack, Structure

public class StackVector
extends AbstractStack
implements Stack

An implementation of a stack, based on extensible arrays. The head of the stack is stored in the first position 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 which occasionally takes a time proportional to the its length to expand.

Example usage:

To reverse a string with a stack array, we would use the following:

 public static void main(String[] arguments)
 {
     if(arguments.length > 0){
	   StackVector reverseStack = new StackVector();
	   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, StackList, StackArray, AbstractStack

Field Summary
protected  Vector data
          The vector containing the stack data.
 
Constructor Summary
StackVector()
          Construct an empty stack.
StackVector(int size)
          Construct a stack with initial capacity Vector will grow if the stack fills vector.
 
Method Summary
 void add(Object item)
          Add an element from the top of the stack.
 void clear()
          Remove all elements from stack.
 Object get()
          Fetch a reference to the top element of the stack.
 boolean isEmpty()
          Returns true iff the stack is empty.
 Iterator iterator()
          Returns an iterator for traversing the structure.
 Object remove()
          Remove an element from the top of the stack.
 int size()
          Determine the number of elements in stack.
 String toString()
          Construct a string representation of stack.
 
Methods inherited from class structure.AbstractStack
getFirst, peek, pop, push
 
Methods inherited from class structure.AbstractLinear
empty, remove
 
Methods inherited from class structure.AbstractStructure
contains, elements, hashCode, values
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait
 
Methods inherited from interface structure.Stack
empty, getFirst, peek, pop, push
 
Methods inherited from interface structure.Structure
contains, elements, remove, values
 

Field Detail

data

protected Vector data
The vector containing the stack data.
Constructor Detail

StackVector

public StackVector()
Construct an empty stack.

StackVector

public StackVector(int size)
Construct a stack with initial capacity Vector will grow if the stack fills vector.
Parameters:
size - The initial capacity of the vector.
Method Detail

add

public void add(Object item)
Add an element from the top of the stack.
Specified by:
add in interface Stack
Parameters:
item - The element to be added to the stack top.
Postcondition:
item is added to stack will be popped next if no intervening add

remove

public Object remove()
Remove an element from the top of the stack.
Specified by:
remove in interface Stack
Precondition:
stack is not empty
Postcondition:
most recently added item is removed and returned
Returns:
The item removed from the top of the stack.
See Also:
AbstractStack.pop()

get

public Object get()
Fetch a reference to the top element of the stack.
Specified by:
get in interface Stack
Precondition:
stack is not empty
Postcondition:
top value (next to be popped) is returned
Returns:
A reference to the top element of the stack.

isEmpty

public boolean isEmpty()
Returns true iff the stack is empty. Provided for compatibility with java.util.Vector.empty.
Specified by:
isEmpty in interface Structure
Overrides:
isEmpty in class AbstractStructure
Postcondition:
returns true if and only if the stack is empty
Returns:
True iff the stack is empty.

size

public int size()
Determine the number of elements in stack.
Specified by:
size in interface Stack
Postcondition:
returns the number of elements in stack
Returns:
The number of elements in stack.

clear

public void clear()
Remove all elements from stack.
Specified by:
clear in interface Structure
Postcondition:
removes all elements from stack

iterator

public Iterator iterator()
Description copied from interface: Structure
Returns an iterator for traversing the structure.
Specified by:
iterator in interface Structure
Following copied from interface: structure.Structure
Returns:
an iterator for traversing the structure
See Also:
AbstractIterator, Iterator, Enumeration, Structure.elements()

toString

public String toString()
Construct a string representation of stack.
Overrides:
toString in class Object
Postcondition:
returns a string representation of stack
Returns:
A string representing the stack.

© 1998-2002 McGraw-Hill