// File: LongLinkedStack.java from the package edu.colorado.collections // Complete documentation is available from the LongLinkedStack link in: // http://www.cs.colorado.edu/~main/docs/ package edu.colorado.collections; import java.util.EmptyStackException; import edu.colorado.nodes.LongNode; /****************************************************************************** * A LongLinkedStack is a stack of references to objects. * * Limitations: * Beyond Int.MAX_VALUE items, size is wrong. * * Java Source Code for this class: * * http://www.cs.colorado.edu/~main/edu/colorado/collections/LongLinkedStack.java * * * @author Michael Main * (main@colorado.edu) * * @version Feb 10, 2016 * * @see LongStack * @see ObjectLinkedStack * @see BooleanLinkedStack * @see ByteLinkedStack * @see CharLinkedStack * @see DoubleLinkedStack * @see FloatLinkedStack * @see IntLinkedStack * @see ShortLinkedStack ******************************************************************************/ public class LongLinkedStack implements Cloneable { // Invariant of the LongLinkedStack class: // 1. The items in the stack are stored in a linked list, with the top of // the stack stored at the head node, down to the bottom of the stack // at the final node. // 2. The instance variable top is the head reference of the linked list // of items. private LongNode top; /** * Initialize an empty stack. * Postcondition: * This stack is empty. **/ public LongLinkedStack( ) { top = null; } /** * Generate a copy of this stack. * @return * The return value is a copy of this stack. Subsequent changes to the * copy will not affect the original, nor vice versa. Note that the return * value must be type cast to a LongLinkedStack before it can be used. * @exception OutOfMemoryError * Indicates insufficient memory for creating the clone. **/ public Object clone( ) { // Clone a LongLinkedStack. LongLinkedStack answer; try { answer = (LongLinkedStack) super.clone( ); } catch (CloneNotSupportedException e) { // This exception should not occur. But if it does, it would probably indicate a // programming error that made super.clone unavailable. The most comon error // The most common error would be forgetting the "Implements Cloneable" // clause at the start of this class. throw new RuntimeException ("This class does not implement Cloneable"); } answer.top = LongNode.listCopy(top); return answer; } /** * Determine whether this stack is empty. * @return * true if this stack is empty; * false otherwise. **/ public boolean isEmpty( ) { return (top == null); } /** * Get the top item of this stack, without removing the item. * Precondition: * This stack is not empty. * @return * the top item of the stack * @exception EmptyStackException * Indicates that this stack is empty. **/ public long peek( ) { if (top == null) // EmptyStackException is from java.util and its constructor has no argument. throw new EmptyStackException( ); return top.getData( ); } /** * Get the top item, removing it from this stack. * Precondition: * This stack is not empty. * @return * The return value is the top item of this stack, and the item has * been removed. * @exception EmptyStackException * Indicates that this stack is empty. **/ public long pop( ) { long answer; if (top == null) // EmptyStackException is from java.util and its constructor has no argument. throw new EmptyStackException( ); answer = top.getData( ); top = top.getLink( ); return answer; } /** * Push a new item onto this stack. The new item may be the null * reference. * @param item * the item to be pushed onto this stack * Postcondition: * The item has been pushed onto this stack. * @exception OutOfMemoryError * Indicates insufficient memory for increasing the stack's capacity. **/ public void push(long item) { top = new LongNode(item, top); } /** * Accessor method to determine the number of items in this stack. * @return * the number of items in this stack **/ public int size( ) { return LongNode.listLength(top); } }