Documentation of 'edu.princeton.cs.algs4.ResizingArrayStack' Java class
ResizingArrayStack
edu.princeton.cs.algs4

Class ResizingArrayStack<Item>

  • All Implemented Interfaces:
    java.lang.Iterable<Item>


    public class ResizingArrayStack<Item>
    extends java.lang.Object
    implements java.lang.Iterable<Item>
    The ResizingArrayStack 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.

    This implementation uses a resizing array, which double the underlying array when it is full and halves the underlying array when it is one-quarter full. The push and pop operations take constant amortized time. The size, peek, and is-empty operations takes constant time in the worst case.

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

    • Constructor Summary

      Constructors 
      Constructor and Description
      ResizingArrayStack()
      Initializes an empty stack.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method and Description
      boolean isEmpty()
      Is this stack empty?
      java.util.Iterator<Item> iterator()
      Returns an iterator to this stack that iterates through the items in LIFO order.
      static void main(java.lang.String[] args)
      Unit tests the Stack data type.
      Item peek()
      Returns (but does not remove) the item most recently added to this stack.
      Item pop()
      Removes and returns the item most recently added to this stack.
      void push(Item item)
      Adds the item to this stack.
      int size()
      Returns the number of items in the stack.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • ResizingArrayStack

        public ResizingArrayStack()
        Initializes an empty stack.
    • Method Detail

      • isEmpty

        public boolean isEmpty()
        Is this stack empty?
        Returns:
        true if this stack is empty; false otherwise
      • size

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

        public void push(Item item)
        Adds the item to this stack.
        Parameters:
        item - the item to add
      • pop

        public Item pop()
        Removes and returns the item most recently added to this stack.
        Returns:
        the item most recently added
        Throws:
        java.util.NoSuchElementException - if this stack is empty
      • peek

        public Item peek()
        Returns (but does not remove) the item most recently added to this stack.
        Returns:
        the item most recently added to this stack
        Throws:
        java.util.NoSuchElementException - if this stack is empty
      • iterator

        public java.util.Iterator<Item> iterator()
        Returns an iterator to this stack that iterates through the items in LIFO order.
        Specified by:
        iterator in interface java.lang.Iterable<Item>
        Returns:
        an iterator to this stack that iterates through the items in LIFO order.
      • main

        public static void main(java.lang.String[] args)
        Unit tests the Stack data type.
        Parameters:
        args - the command-line arguments

DataMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.