Class ArrayStack<E>

java.lang.Object
com.zybooks.dsaj.stackqueue.ArrayStack<E>
Type Parameters:
E - the element type
All Implemented Interfaces:
Stack<E>

public class ArrayStack<E> extends Object implements Stack<E>
Implementation of the stack ADT using a fixed-length array. All operations are performed in constant time. An exception is thrown if a push operation is attempted when the size of the stack is equal to the length of the array.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default array capacity.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty stack using the default array capacity.
    ArrayStack(int capacity)
    Constructs and empty stack with the given array capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Tests whether the stack is empty.
    static void
    main(String[] args)
    Demonstrates sample usage of a stack.
    pop()
    Removes and returns the top element from the stack.
    void
    push(E e)
    Adds an element at the top of the stack.
    int
    Returns the number of elements in the stack.
    top()
    Returns, but does not remove, the element at the top of the stack.
    Produces a string representation of the contents of the stack.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • ArrayStack

      public ArrayStack()
      Constructs an empty stack using the default array capacity.
    • ArrayStack

      public ArrayStack(int capacity)
      Constructs and empty stack with the given array capacity.
      Parameters:
      capacity - length of the underlying array
  • Method Details

    • size

      public int size()
      Returns the number of elements in the stack.
      Specified by:
      size in interface Stack<E>
      Returns:
      number of elements in the stack
    • isEmpty

      public boolean isEmpty()
      Tests whether the stack is empty.
      Specified by:
      isEmpty in interface Stack<E>
      Returns:
      true if the stack is empty, false otherwise
    • push

      public void push(E e) throws IllegalStateException
      Adds an element at the top of the stack.
      Specified by:
      push in interface Stack<E>
      Parameters:
      e - the element to be added
      Throws:
      IllegalStateException - if the array storing the elements is full
    • top

      public E top()
      Returns, but does not remove, the element at the top of the stack.
      Specified by:
      top in interface Stack<E>
      Returns:
      top element in the stack (or null if empty)
    • pop

      public E pop()
      Removes and returns the top element from the stack.
      Specified by:
      pop in interface Stack<E>
      Returns:
      element removed (or null if empty)
    • toString

      public String toString()
      Produces a string representation of the contents of the stack. (ordered from top to bottom). This exists for debugging purposes only.
      Overrides:
      toString in class Object
      Returns:
      textual representation of the stack
    • main

      public static void main(String[] args)
      Demonstrates sample usage of a stack.