m865.datastructures
Class StackLL<T>

java.lang.Object
  extended by m865.datastructures.AbstractStack<T>
      extended by m865.datastructures.StackLL<T>
All Implemented Interfaces:
java.lang.Iterable<T>, java.util.Collection<T>

public class StackLL<T>
extends AbstractStack<T>

This class implements a Stack using a simple Linked List. Version 3.0 incorporates generics


Nested Class Summary
protected  class StackLL.Link<T>
          The Linked List is built from a simple forward link.
 class StackLL.StackLLIterator<T>
          An iterator for an Linked List stack.
 
Field Summary
protected  int nItems
           
protected  StackLL.Link<T> top
          The stack is implemented with a Linked List The Linked List starts with the link called top.
 
Fields inherited from class m865.datastructures.AbstractStack
hash
 
Constructor Summary
  StackLL()
          Constructs a stack which uses a simple Linked List.
  StackLL(java.util.Collection<? extends T> c)
          Constructs a stack which is initialized with the objects in the specified collection
protected StackLL(int hash, int n, StackLL.Link<T> topOfLinkedList)
          Constructs a stack with a specified hash code and a specified linked list.
 
Method Summary
 void clear()
          Removes all the objects from this stack.
 boolean isEmpty()
          Determines whether the stack is empty.
 java.util.Iterator<T> iterator()
          A factory method which returns an Iterator to the collection in this stack.
static void main(java.lang.String[] args)
          This main method tests the StackLL class to insure that the elementary functions are correct.
 T peek()
          Returns the object on the top of the stack.
 T pop()
          Removes and returns the object on the top of the stack.
 void push(T x)
          Pushes an object onto the top of the stack.
 int size()
          Determines the size of this stack.
 java.lang.String toString()
          List the objects in the stack
 
Methods inherited from class m865.datastructures.AbstractStack
add, addAll, contains, containsAll, downdateHashCode, equals, hashCode, remove, removeAll, retainAll, toArray, toArray, updateHashCode
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

top

protected StackLL.Link<T> top
The stack is implemented with a Linked List The Linked List starts with the link called top.


nItems

protected int nItems
Constructor Detail

StackLL

public StackLL()
Constructs a stack which uses a simple Linked List.


StackLL

public StackLL(java.util.Collection<? extends T> c)
Constructs a stack which is initialized with the objects in the specified collection

Parameters:
c - the collection of objects to be initially pushed onto this stack.

StackLL

protected StackLL(int hash,
                  int n,
                  StackLL.Link<T> topOfLinkedList)
Constructs a stack with a specified hash code and a specified linked list. This constuctor is only for use within the class and its subclasses.

Parameters:
hash - the hash code
topOfLinkedList - the top of the linked list.
Method Detail

push

public void push(T x)
Pushes an object onto the top of the stack.

Specified by:
push in class AbstractStack<T>
Parameters:
x - the object to be placed on the top of the stack.

pop

public T pop()
Removes and returns the object on the top of the stack.

Specified by:
pop in class AbstractStack<T>
Returns:
the object on the top of the stack or null if the stack is empty.

peek

public T peek()
Returns the object on the top of the stack. The stack remains unchanged.

Specified by:
peek in class AbstractStack<T>
Returns:
the object on the top of the stack or null if the stack is empty.

clear

public void clear()
Removes all the objects from this stack. This is one of the optional operations specified by the Collection Interface


isEmpty

public boolean isEmpty()
Determines whether the stack is empty.

Specified by:
isEmpty in interface java.util.Collection<T>
Overrides:
isEmpty in class AbstractStack<T>
Returns:
true - if this collection contains no elements.

iterator

public java.util.Iterator<T> iterator()
A factory method which returns an Iterator to the collection in this stack. This iterator will walk throught the stack from the top to the bottom. It will throw a ConcurrentModificationException if the stack is altered during the iteration.

Returns:
a StackLLIterator.

size

public int size()
Determines the size of this stack.

Returns:
the number of objects in this stack.

toString

public java.lang.String toString()
List the objects in the stack

Overrides:
toString in class AbstractStack<T>
Returns:
a formatted string listing the objects in this stack

main

public static void main(java.lang.String[] args)
This main method tests the StackLL class to insure that the elementary functions are correct.

Parameters:
args - optional command line arguments which will be ignored.