Package dsa

Class LinkedStack<T>

java.lang.Object
dsa.LinkedStack<T>
Type Parameters:
T - the type of items in the stack.
All Implemented Interfaces:
Stack<T>, Iterable<T>

public class LinkedStack<T> extends Object implements Stack<T>
This data type provides an implementation of the Stack API, using a linked-list as the underlying data structure.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty stack.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Returns true if this stack is empty, and false otherwise.
    Returns an iterator to iterate over the items in this stack in LIFO order.
    static void
    main(String[] args)
    Unit tests the data type.
    Returns the item at the top of this stack.
    pop()
    Removes and returns the item at the top of this stack.
    void
    push(T item)
    Adds item to the top of this stack.
    int
    Returns the number of items in this stack.
    Returns a string representation of this stack.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Constructor Details

    • LinkedStack

      public LinkedStack()
      Constructs an empty stack.
  • Method Details

    • isEmpty

      public boolean isEmpty()
      Returns true if this stack is empty, and false otherwise.
      Specified by:
      isEmpty in interface Stack<T>
      Returns:
      true if this stack is empty, and false otherwise.
    • size

      public int size()
      Returns the number of items in this stack.
      Specified by:
      size in interface Stack<T>
      Returns:
      number of items in this stack.
    • push

      public void push(T item)
      Adds item to the top of this stack.
      Specified by:
      push in interface Stack<T>
      Parameters:
      item - the item.
    • peek

      public T peek()
      Returns the item at the top of this stack.
      Specified by:
      peek in interface Stack<T>
      Returns:
      item at the top of this stack.
    • pop

      public T pop()
      Removes and returns the item at the top of this stack.
      Specified by:
      pop in interface Stack<T>
      Returns:
      item at the top of this stack.
    • toString

      public String toString()
      Returns a string representation of this stack.
      Specified by:
      toString in interface Stack<T>
      Overrides:
      toString in class Object
      Returns:
      a string representation of this stack.
    • iterator

      public Iterator<T> iterator()
      Returns an iterator to iterate over the items in this stack in LIFO order.
      Specified by:
      iterator in interface Iterable<T>
      Returns:
      an iterator to iterate over the items in this stack in LIFO order.
    • main

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