Package algs13

Class Stack<T>

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

public class Stack<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.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private class 
     
    private static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private Stack.Node<T>
     
    private int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create an empty stack.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static <T> boolean
    check(Stack<T> that)
     
    boolean
    Is the stack empty?
    Return an iterator to the stack that iterates through the items in LIFO order.
    static void
    main(String[] args)
    A test client.
    static void
    main1(String[] args)
     
    static void
    main2(String[] args)
     
    Return the item most recently added to the stack.
    pop()
    Delete and return the item most recently added to the stack.
    void
    push(T item)
    Add the item to the stack.
    int
    Return the number of items in the stack.
    Return string representation.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

  • Constructor Details

    • Stack

      public Stack()
      Create an empty stack.
  • 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
    • check

      private static <T> boolean check(Stack<T> that)
    • 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>
    • main

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

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

      public static void main2(String[] args)