public class LinkedBag<Item> extends Object implements Iterable<Item>
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.
| Constructor and Description |
|---|
LinkedBag()
Initializes an empty bag.
|
| Modifier and Type | Method and 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 the LinkedBag data type.
|
int |
size()
Returns the number of items in this bag.
|
public boolean isEmpty()
public int size()
public void add(Item item)
item - the item to add to this bagpublic Iterator<Item> iterator()
public static void main(String[] args)
Copyright © 2014. All Rights Reserved.