Package edu.princeton.cs.algs4
Class LinkedBag<Item>
- Object
-
- edu.princeton.cs.algs4.LinkedBag<Item>
-
- All Implemented Interfaces:
Iterable<Item>
public class LinkedBag<Item> extends Object implements Iterable<Item>
TheLinkedBag
class represents a bag (or multiset) of generic items. It supports insertion and iterating over the items in arbitrary order.This implementation uses a singly linked list with a non-static nested class Node. See
Bag
for a version that uses a static nested class. The add, isEmpty, and size operations take constant time. Iteration takes time proportional to the number of items.For additional documentation, see Section 1.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
- Author:
- Robert Sedgewick, Kevin Wayne
-
-
Constructor Summary
Constructors Constructor Description LinkedBag()
Initializes an empty bag.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(Item item)
Adds the item to this bag.boolean
isEmpty()
Is this bag empty?Iterator<Item>
iterator()
Returns an iterator that iterates over the items in the bag.static void
main(String[] args)
Unit tests theLinkedBag
data type.int
size()
Returns the number of items in this bag.-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Is this bag empty?- Returns:
- true if this bag is empty; false otherwise
-
size
public int size()
Returns the number of items in this bag.- Returns:
- the number of items in this bag
-
add
public void add(Item item)
Adds the item to this bag.- Parameters:
item
- the item to add to this bag
-
iterator
public Iterator<Item> iterator()
Returns an iterator that iterates over the items in the bag.
-
main
public static void main(String[] args)
Unit tests theLinkedBag
data type.- Parameters:
args
- the command-line arguments
-
-