Class LinkedStack<T>

java.lang.Object
LinkedStack<T>
Type Parameters:
T - the type of data contained in the stack
All Implemented Interfaces:
StackADT<T>

public class LinkedStack<T> extends Object implements StackADT<T>
A generic singly-linked stack implementation.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private LinkedNode<T>
    A reference to the LinkedNode currently at the top of the stack, which is null when the stack is empty.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    contains(T value)
    Returns true if this stack contains the element, false otherwise
    Creates a copy of the current contents of this stack in the order they are present here, in ArrayList form.
    boolean
    Returns true if this stack contains no elements.
    Accesses the value added to this stack most recently, without modifying the stack
    pop()
    Removes and returns the value added to this stack most recently
    void
    push(T value)
    Add a new element to the top of this stack, assumed to be non-null.

    Methods inherited from class java.lang.Object

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

    • top

      private LinkedNode<T> top
      A reference to the LinkedNode currently at the top of the stack, which is null when the stack is empty.
  • Method Details

    • push

      public void push(T value)
      Description copied from interface: StackADT
      Add a new element to the top of this stack, assumed to be non-null.
      Specified by:
      push in interface StackADT<T>
      Parameters:
      value - the value to add
    • pop

      public T pop()
      Description copied from interface: StackADT
      Removes and returns the value added to this stack most recently
      Specified by:
      pop in interface StackADT<T>
      Returns:
      the most recently-added value, or null if the stack is empty
    • peek

      public T peek()
      Description copied from interface: StackADT
      Accesses the value added to this stack most recently, without modifying the stack
      Specified by:
      peek in interface StackADT<T>
      Returns:
      the most recently-added value, or null if the stack is empty
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: StackADT
      Returns true if this stack contains no elements.
      Specified by:
      isEmpty in interface StackADT<T>
      Returns:
      true if the stack contains no elements, false otherwise
    • contains

      public boolean contains(T value)
      Description copied from interface: StackADT
      Returns true if this stack contains the element, false otherwise
      Specified by:
      contains in interface StackADT<T>
      Parameters:
      value - the value to check
      Returns:
      true if the stack contains the element, false otherwise
    • getList

      public ArrayList<T> getList()
      Creates a copy of the current contents of this stack in the order they are present here, in ArrayList form. This method should traverse the stack without removing any elements, and add the values (not the nodes!) to an ArrayList in the order they appear in the stack, with the top of the stack at index 0.
      Returns:
      an ArrayList representation of the current state of this stack