Class LinkedStack<Item>

java.lang.Object
dsa.lab03.solutions.LinkedStack<Item>
Type Parameters:
Item - the item type
All Implemented Interfaces:
Container<Item>, Stack<Item>, Iterable<Item>

public class LinkedStack<Item> extends Object implements Stack<Item>
A linked stack.

Implements the stack interface by using a singly-linked list.

  • Constructor Details

    • LinkedStack

      public LinkedStack()
      Construct an empty linked stack.
    • LinkedStack

      public LinkedStack(Iterable<Item> items)
      Construct a linked stack containing the given items.
      Parameters:
      items - the items
    • LinkedStack

      @SafeVarargs public LinkedStack(Item... items)
      Construct a linked stack containing the given items.
      Parameters:
      items - the items
  • Method Details

    • push

      public void push(Item item)
      Description copied from interface: Stack
      Push (insert) the given item onto the top.
      Specified by:
      push in interface Stack<Item>
      Parameters:
      item - the new top item
    • pop

      public Item pop() throws NoSuchElementException
      Description copied from interface: Stack
      Pop (remove and return) the given item from the top.
      Specified by:
      pop in interface Stack<Item>
      Returns:
      the old top item
      Throws:
      NoSuchElementException - if there's no top item to remove (i.e. if this is empty)
    • top

      public Item top() throws NoSuchElementException
      Description copied from interface: Stack
      Get the item on top.
      Specified by:
      top in interface Stack<Item>
      Returns:
      the top item
      Throws:
      NoSuchElementException - if there's no top item to retrieve (i.e. if this is empty)
    • size

      public int size()
      Description copied from interface: Container
      Get the number of contained items.
      Specified by:
      size in interface Container<Item>
      Returns:
      the size
    • items

      public Iterable<Item> items()
      Description copied from interface: Container
      Get an iterable that yields each item once.
      Specified by:
      items in interface Container<Item>
      Returns:
      an iterable over the items