© 1998-2002 McGraw-Hill

structure
Class StackList

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

public class StackList
extends AbstractStack
implements Stack

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

Field Summary
protected  List data
          The list that maintains the stack data.
 
Constructor Summary
StackList()
          Construct an empty stack.
 
Method Summary
 void add(Object 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.
 Object get()
          Get a reference to the top value in the stack.
 Iterator iterator()
          Returns an iterator for traversing the structure.
 Object remove()
          Remove a value from the top of the stack.
 int size()
          Determine the number of elements in the stack.
 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
, clone, equals, finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait
 
Methods inherited from interface structure.Stack
getFirst, peek, pop, push
 
Methods inherited from interface structure.Structure
contains, elements, isEmpty, remove, values
 

Field Detail

data

protected List data
The list that maintains the stack data.
Constructor Detail

StackList

public StackList()
Construct an empty stack.
Method Detail

clear

public void clear()
Remove all elements from the stack.
Specified by:
clear in interface Structure
Postcondition:
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 Stack
Overrides:
empty in class AbstractLinear
Postcondition:
returns true iff the stack is empty
Returns:
True iff the stack is empty.
See Also:
AbstractStructure.isEmpty()

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()

get

public Object get()
Get a reference to the top value in the stack.
Specified by:
get in interface Stack
Precondition:
stack is not empty
Postcondition:
returns the top element (most recently pushed) from stack
Returns:
A reference to the top element of the top of the stack.

add

public void add(Object value)
Add a value to the top of the stack.
Specified by:
add in interface Stack
Parameters:
item - The value to be added.
Postcondition:
adds an element to stack; will be next element popped if no intervening push
See Also:
AbstractStack.push(java.lang.Object)

remove

public Object remove()
Remove a value from the top of the stack.
Specified by:
remove in interface Stack
Precondition:
stack is not empty
Postcondition:
removes and returns the top element from stack
Returns:
The value removed from the top of the stack.
See Also:
AbstractStack.pop()

size

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

toString

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

© 1998-2002 McGraw-Hill