Class ListStack<E>

  • All Implemented Interfaces:
    Stack<E>

    public class ListStack<E>
    extends java.lang.Object
    implements Stack<E>
    Provides an implementation of a Stack using a singly linked list:

    All operations require constant time in the worst case.
    • Constructor Summary

      Constructors 
      Constructor Description
      ListStack()
      Creates a linked list representing an empty stack.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isEmpty()
      Reports whether the stack is currently empty without changing it.
      E peek()
      Reports the top element of the stack without changing it.
      E pop()
      Removes an element from the top of the stack and reports it.
      void push​(E x)
      Pushes a new entry onto the stack.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ListStack

        public ListStack()
        Creates a linked list representing an empty stack.
    • Method Detail

      • push

        public void push​(E x)
        Description copied from interface: Stack
        Pushes a new entry onto the stack.
        Specified by:
        push in interface Stack<E>
        Parameters:
        x - the object to be pushed onto the stack

        Precondition:
        1. The Stack Invariant is satisfied.
        2. An element x of type E has been given as input.
        Postcondition:
        1. The Stack Invariant is satisfied.
        2. The input object x has been pushed onto the top of the stack (which is otherwise unchanged).
      • peek

        public E peek()
        Description copied from interface: Stack
        Reports the top element of the stack without changing it.
        Specified by:
        peek in interface Stack<E>
        Returns:
        the element at the top of the stack
      • pop

        public E pop()
        Description copied from interface: Stack
        Removes an element from the top of the stack and reports it.
        Specified by:
        pop in interface Stack<E>
        Returns:
        the element removed from the stack
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: Stack
        Reports whether the stack is currently empty without changing it.
        Specified by:
        isEmpty in interface Stack<E>
        Returns:
        true if the stack is empty, and false otherwise

        Precondition:
        1. The Stack Invariant is satisfied.
        Postcondition:
        1. The Stack Invariant is satisfied.
        2. The stack is not changed.
        3. If the stack was empty before this operation then “true” is returned as output.
        4. If the stack was not empty before this operation then “false” is returned as output.