Package dsa

Class LinkedBag<T>

java.lang.Object
dsa.LinkedBag<T>
Type Parameters:
T - the type of items in the bag.
All Implemented Interfaces:
Bag<T>, Iterable<T>

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

    • LinkedBag

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

    • isEmpty

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

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

      public void add(T item)
      Adds item to this bag.
      Specified by:
      add in interface Bag<T>
      Parameters:
      item - the item.
    • toString

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

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

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