Package algs13

Class StackWithNonStaticNode<T>

java.lang.Object
algs13.StackWithNonStaticNode<T>
All Implemented Interfaces:
Iterable<T>

public class StackWithNonStaticNode<T> extends Object implements Iterable<T>
The Stack class represents a last-in-first-out (LIFO) stack of generic items. It supports the usual push and pop operations, along with methods for peeking at the top item, testing if the stack is empty, and iterating through the items in LIFO order.

All stack operations except iteration are constant time.

For additional documentation, see Section 1.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

  • Constructor Details

  • Method Details

    • isEmpty

      public boolean isEmpty()
      Is the stack empty?
    • size

      public int size()
      Return the number of items in the stack.
    • push

      public void push(T item)
      Add the item to the stack.
    • pop

      public T pop()
      Delete and return the item most recently added to the stack.
      Throws:
      NoSuchElementException - if stack is empty.
    • peek

      public T peek()
      Return the item most recently added to the stack.
      Throws:
      NoSuchElementException - if stack is empty.
    • toString

      public String toString()
      Return string representation.
      Overrides:
      toString in class Object
    • iterator

      public Iterator<T> iterator()
      Return an iterator to the stack that iterates through the items in LIFO order.
      Specified by:
      iterator in interface Iterable<T>
    • bookMain

      public static void bookMain(String[] args)
      A test client.
    • main

      public static void main(String[] args)
    • main2

      public static void main2(String[] args)