codesimian
Class StackNeverEmpty<K>

java.lang.Object
  extended by codesimian.StackNeverEmpty<K>

public class StackNeverEmpty<K>
extends java.lang.Object

A stack that holds its first object constant so it can never become empty. You can change the bottom object, but only with

Can use the new "generics" syntax for types. Example:
StackNeverEmpty stringStack = new StackNeverEmpty("bottom string object");
String s = stringStack.pop(); //no cast
//pop returns the bottom String but does not remove it

Implemented as a singly-linked list.

This should become a subclass of CS.

If you want a stack of Number's or CS's, use a NumberStack.NeverEmpty instead.

See Also:
NumberStack

Constructor Summary
StackNeverEmpty(K pushMe)
          pushMe permanently becomes the lowest object in this stack
 
Method Summary
 K peek()
          Returns the object on top of the stack
 K pop()
          Returns the object at the top of the stack.
 void push(K addMe)
           
 K replace(K replaceTopWithMe)
          Replaces the top object with replaceTopWithMe and returns the removed object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StackNeverEmpty

public StackNeverEmpty(K pushMe)
pushMe permanently becomes the lowest object in this stack

Method Detail

push

public void push(K addMe)

pop

public K pop()
Returns the object at the top of the stack. If there are at least 2 objects left, also removes the returned object. The stack can never become empty. Pop would repeatedly return the bottom object if there is only 1 object.


peek

public K peek()
Returns the object on top of the stack


replace

public K replace(K replaceTopWithMe)
Replaces the top object with replaceTopWithMe and returns the removed object.

This stack can never be empty, so the only way to change the bottom object is to REPLACE it.