The Bag ADT [DSAUP]
What is a Bag?
A bag:
- is a collection of things
- allows duplicates
- isn't ordered but the items should be comparable
The Interface
Method | Effect |
---|---|
Bag() | Creates an empty bag |
length() | Returns the number of items in the bag |
contains(item) | Returns True if item is in the bag |
add(item) | Adds item to the bag |
remove(item) | Remove and return item from the bag or raise exception if it's not in the bag |
iterator() | Create and return iterator to iterate over the bag |