geneseo.cs.sc
Class Stack

java.lang.Object
  extended bygeneseo.cs.sc.List
      extended bygeneseo.cs.sc.Stack
All Implemented Interfaces:
java.io.Serializable

public class Stack
extends List

Represents stacks of arbitrary objects. This class reflects a view of a stack as a kind of list, albeit one with some unique operations (push, pop, etc.), and in which some of the traditional list operations (find, delete, etc.) do nothing. The overall result is a class whose instances behave like standard stacks.

This class was created to support the text Algorithms & Data Structures: The Science of Computing by Doug Baldwin and Greg Scragg. All references herein to "the text" refer to that book. This stack class is the one described in Chapter 12, including full implementations of some methods that were "left to an exercise" in the text.

See Also:
Serialized Form

Constructor Summary
Stack()
          Initialize an empty stack.
 
Method Summary
 void concat(List extraList)
          Warns the user that concatenation is not a standard stack operation.
 void delete(java.lang.Object value)
          Warns the user that general deletion is not a standard stack operation.
 boolean find(java.lang.Object target)
          Warns the user that searching is not a standard stack operation.
 List makeNewList()
          Create a new list that is an instance of Stack, as required of any subclass of List.
 java.lang.Object pop()
          Retrieve and remove the top object from a stack.
 void printStack()
          Print a stack.
 void push(java.lang.Object newItem)
          Adds an object to a stack.
 
Methods inherited from class geneseo.cs.sc.List
addItem, copy, getAndRemove, getFirst, getRest, isEmpty, printList, printListForward, removeItem, restore, save, setFirstItem, setRest, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Stack

public Stack()
Initialize an empty stack. For example

Stack s = new Stack();

Method Detail

makeNewList

public List makeNewList()
Create a new list that is an instance of Stack, as required of any subclass of List. Clients of Stack generally do not send this message themselves, but code inside inherited methods relies on it.

Overrides:
makeNewList in class List
Returns:
A new, empty, Stack object

push

public void push(java.lang.Object newItem)
Adds an object to a stack. The added object becomes the stack's top element. For example

s.push( "hello" );

Parameters:
newItem - The object to add to the stack.

pop

public java.lang.Object pop()
Retrieve and remove the top object from a stack. For example,

Object top = s.pop();

This message has a precondition that the stack is not empty.

Returns:
The top item from the stack.

printStack

public void printStack()
Print a stack. For example

s.printStack();


concat

public void concat(List extraList)
Warns the user that concatenation is not a standard stack operation.

Overrides:
concat in class List
Parameters:
extraList - The list to be added to the end of the current list.

find

public boolean find(java.lang.Object target)
Warns the user that searching is not a standard stack operation.

Overrides:
find in class List
Parameters:
target - The object to look for in the list.
Returns:
false, indicating that all attempts to search stacks fail.

delete

public void delete(java.lang.Object value)
Warns the user that general deletion is not a standard stack operation.

Overrides:
delete in class List
Parameters:
value - The object to remove from the list.