CS 368 Program 3: More Member Functions for the SortedList Class

What to Write

Extend the SortedList class you wrote for program #2 by adding:

Also, write a main function to test your new version of the SortedList class. Your main function should demonstrate that adding definitions of the SortedList destructor, copy constructor, and operator= makes a difference. In particular, you should use purify to create two instrumented executables; one version should use the definition of the SortedList class from program 2, and the other should use the new definition of the SortedList class (that includes a destructor, copy constructor, and operator=). Both versions should use your new main function. When the first version is run, purify should give a warning about storage leaks, but there should be no warnings for the second version. Also, either purify should report errors in the first version (but not the second version), or there should be a difference in output between the first and second versions, to demonstrate that including a copy constructor and a definition of operator= in the second version is important.

Using Auxiliary Functions

Note that both the destructor and operator= must return the nodes of a linked list to free storage, and both the copy constructor and operator= must make a copy of an existing linked list. Therefore, it is a good idea to write two auxiliary functions, e.g., FreeList and CopyList; function FreeList will be called both by the destructor function and operator=, and CopyList will be called both by the copy constructor and operator=. Here is some advice on how to write the two auxiliary functions: (For an example of a recursive function that processes a linked list, see the 5th "Test Yourself Now" exercise in the "Intro to C++" on-line notes.)

Note that as described, the two auxiliary functions work only on linked lists, not on SortedLists. So they should be static (private) member functions of the SortedList class. Here's how you would declare them inside the SortedList class declaration:

When you write the two functions in SortedList.C, do not include the word "static".

Another possibility for CopyList would be to make it a non-static member function of the SortedList class. If S is a SortedList, and L is a pointer to the first node of a linked list, then the call S.CopyList(L) would use recursion to insert the strings in L in right-to-left order into S. (The function would also work if you made it non-recursive, and inserted the strings in L in left-to-right order, but that would be inefficient, because each Insert operation would add a new value to the end of S's list.)

What to Hand In

Hand in your (extended) SortedList.h and SortedList.C files, as well as your main program (in main.C) by uploading the files.
What to turn-in: SortedList.C