// The Science of Computing Stack class. // History: // August 2003 -- Created by Greg Scragg. // October 2003 -- updates to several methods by GWS // February 2004 -- updated references and variable names to correspond to text changes. // May 2004 -- Documentation and Java usage cleaned up by Doug Baldwin. // November 2004 -- Method name "makeNewObject" corrected to "makeNewList" // by Doug Baldwin. package geneseo.cs.sc; /** * 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.
*/ public class Stack extends List { /** * Initialize an empty stack. For example *Stack s = new Stack();
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.
* @return A new, empty, Stack
object
*/
public List makeNewList() {
return new Stack();
}
/**
* Adds an object to a stack. The added object becomes the stack's
* top element. For example
* s.push( "hello" );
Object top = s.pop();
This message has a precondition that the stack is not empty.
* @return The top item from the stack. */ public Object pop() { Object temp = this.getFirst(); this.removeItem(); return temp; } /** * Print a stack. For example *s.printStack();
false
, indicating that all attempts to search stacks fail.
*/
public boolean find(Object target) {
System.err.println( "Searching not allowed on stacks" );
return false;
}
/**
* Warns the user that general deletion is not a standard stack operation.
*/
public void delete(Object value) {
System.err.println( "General deletion not allowed on stacks" );
}
}