Package dsa

Class LinkedBag<Item>

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

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

      • LinkedBag

        public LinkedBag()
        Constructs an empty bag.
    • Method Detail

      • isEmpty

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

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

        public void add​(Item item)
        Description copied from interface: Bag
        Adds item to this bag.
        Specified by:
        add in interface Bag<Item>
        Parameters:
        item - the item.
      • iterator

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

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

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