Project 9 Extra Notes

Bags

Another name for a "bag" is a "multiset". It's like a set in that the elements are unordered, but unlike a set in that there can be more than one of each element. The C++ STL provides both sets and multisets, but we will not use them in this class.

The assignment specifications do not require you to sort the Bag internally, so don't bother.

Linked Lists

The implementation for our Bag is a a singly linked list. You get to choose whether to use a dummy node or not. If you do, it's possible for to write algorithms that have fewer special cases.

Traversing the Chain

Almost every function will need to traverse, or iterate through the chain. When you iterate, create one or more local pointers. I will call these "iterators", not to be confused with the "iterator" classes found in the STL.

When there is only iterator, such as in display(), call it "curr". When you need another iterator, call it "prev" or "next" and place it accordingly. Usually, the easiest thing to do is start both at the beginning of the chain, and move them along the chain together.

You may also consider using three iterators if it makes things clearer inside remove(), but every function can be done using just two. One strategy is to use just "prev" and "curr" for traversing, then create "next" when you reach your destination.

Use the name "temp" ONLY to refer a a pointer to a newly allocated node.

Relinking

Here, you'll be playing around with the "Succ" member variable. Remember to check for NULL before trying to access a node's data members.

display()

We're serious when we say that you need to display all the information about the object, excluding the data and frequency of the dummy node (if it exists). Make a proper table for the chain. Do this function early and use it to help with testing.

Testing

This project is a little different from data types that we've talked about before, since the user does not control the insert location. However, YOU know how your functions work, so you should construct the driver's insert() and remove operations in such a way that the front, back, and middle of the chain will all be tested. If you don't do this, don't be surprised when our tests uncover mistakes in your algorithms during grading.

As you test, also consider the problem of whether there are one or more instances of a particular element in the Bag. Because of these issues, you will need to have enough commentary in the output that the reader will will understand what's going on.

Finally, try not to overuse display() in your output. It's possible to organize your tests in such a way that you only need to display once for every handful of tests.


Last edited 3/31/13