Class LinkedStack<E>

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

public class LinkedStack<E> extends Object implements Stack<E>
Realization of a stack as an adaptation of a SinglyLinkedList. All operations are performed in constant time.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an initially empty stack.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Tests whether the stack is empty.
    pop()
    Removes and returns the top element from the stack.
    void
    push(E element)
    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
  • Constructor Details

    • LinkedStack

      public LinkedStack()
      Constructs an initially empty stack.
  • 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 element)
      Adds an element at the top of the stack.
      Specified by:
      push in interface Stack<E>
      Parameters:
      element - the element to be added
    • 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