Package dsa

Class LinkedStack<Item>

  • All Implemented Interfaces:
    Stack<Item>, Iterable<Item>

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

      • LinkedStack

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

      • isEmpty

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

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

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

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

        public Item pop()
        Description copied from interface: Stack
        Removes and returns the item at the top of this stack.
        Specified by:
        pop in interface Stack<Item>
        Returns:
        item at the top of this stack.
      • iterator

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

        public String toString()
        Returns a string representation of this stack.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this stack.
      • main

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