package lab.util; import java.util.EmptyStackException; import java.util.NoSuchElementException; /** * A stack data structure that supports O(1) push and pop operations at the top * of the stack. This stack implementation also provides a method to look at the * top element of the stack (peek) without popping the element. * * @param * the element type to store in this stack */ public class Stack { /** * The node holding the top element of the stack (or the * head node of the sequence of nodes). */ private Node top; /** * The size (number of nodes) in the stack. */ private int size; /** * Creates an empty stack (size equal to zero). */ public Stack() { } /** * Returns true if this stack is empty, and false otherwise. * * @return true if this stack is empty, and false otherwise */ public boolean isEmpty() { } /** * Get the size (number of elements) in the stack. * * @return the size (number of elements) in the stack */ public int size() { } /** * Push an element onto the top of the stack. * * @param element * the element to push onto the stack */ public void push(E element) { } /** * Pop an element from the top of the stack returning the element. Throws a * NoSuchElementException if the stack is empty. * * @return the element that was popped off of the top of this stack * @throws NoSuchElementException * if the stack is empty */ public E pop() { } /** * Returns the element that is on the top of this stack without popping it * from the stack. Throws a NoSuchElementException if the stack is empty. * * @return the element that is on the top of this stack * @throws NoSuchElementException * if the stack is empty */ public E peek() { } /** * Compares the specified object to this stack for equality. Two stacks are * equal if they contain the same number of elements in the same order. * * @param obj * the object to compare to this stack for equality * @return true if the specified object is equal to this stack * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { } /** * Returns a hash code for this stack. The hash code is computed using the * elements of this stack. * * @return a hash code for this stack * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; NodeIterator itr = new NodeIterator(this.top); boolean hasNext = itr.hasNext(); while (hasNext) { result = prime * result + itr.next().getElement().hashCode(); hasNext = itr.hasNext(); } return result; } /** * Returns a string representation of this stack. The string representation * consists of a list of the stack's elements from the top of the stack * to the bottom of the stack, enclosed in square brackets ("[]"). Adjacent * elements are separated by the characters ", " (comma and space). Elements * are converted to strings as by String.valueOf(Object). * * @return a string representation of this stack * @see java.lang.Object#toString() */ @Override public String toString() { NodeIterator itr = new NodeIterator(this.top); StringBuilder b = new StringBuilder("["); boolean hasNext = itr.hasNext(); while (hasNext) { Node n = itr.next(); b.append(n.getElement().toString()); hasNext = itr.hasNext(); if (hasNext) { b.append(", "); } } b.append("]"); return b.toString(); } /** * Returns the node at the top of the stack. This method is here for * debugging and testing purposes. * * @return the node at the top of the stack */ public Node getTop() { return this.top; } }