CS 14 - Lab 6
In this lab you are going to add to the List class you developed in
last week's lab to address a deficiency. You will also use a makefile
to compile your program.
Note that the program must be written, compiled, and executed under
Linux.
Create a directory called lab6 and copy the list.cpp and
list.h files from your lab5 directory into it using the
cp command.
The Deficiency
In the List constructor, a sentinel node is allocated (with the
new statement). Also, a new node is allocated any time the
Insert method is called. When are these nodes deallocated (deleted),
however?
When Remove is called, a node is removed from the list (if the value
is found). This node should also be deleted (with the delete
statement). However, what if the List is destroyed (for example, when
the program ends) while it still contains values? When this happens, all of
the nodes still in the list should be deleted. Also, the sentinel node should
be deleted.
Details
Modify your class definition (in list.h) to add a public method
called Clear and a class destructor.
Following are descriptions of the these methods:
void Clear()
This class method takes no parameters and returns nothing. It simply clears
the entire list. In other words, it removes all of the nodes in the
linked list (but not the sentinel node).
As each node is removed from the linked list, print out its value. This is
being done strictly for testing purposes.
Finally, make sure that as each node is removed that you delete it.
~List()
This class destructor should do two things: clear the list (by calling the
clear method) and delete the sentinel node.
bool Remove( int value )
Look at your code for this method. If it is not deleting the node being
removed from the linked list, add code to do so. No other changes to this
method are necessary.
Main Program
Modify your main program (in main.cpp) to include an option to clear
the list. If the user selects this option, call the Clear method to
clear the list.
Compiling With a Makefile
To compile your program, you could use the g++ command, but now that we
have broken up the program into 5 files (main.cpp, student.cpp, student.h,
ssn.cpp, and ssn.h), using g++ directly is getting inconvenient.
Fortunately, there is an alternative: a makefile.
A makefile is simply a textfile that contains commands on how to compile
your program. I have provided a makefile for this lab; click
here to see it. Once you have saved the makefile
in your lab6 directory, you can compile your program by simply typing
in the command make.
When you type make, the compiler will automatically compile each of your
source (i.e. .cpp) files. Assuming there are no errors, it will produce an
executable file called lab6 (not a.out as in the last 2 labs).
To execute your program, then, you simply type in the command lab6.
For more information on makefiles, click
here.
Grading
When you have finished the program, your TA will grade it. To receive full
credit, your TA will check all of the following:
- The program works correctly
- You created the new methods required
- You have a makefile to compile your program
- The program is properly commented. Each function should have a comment
at the beginning explaining what it does, what each of its parameters
are for, and the return value (if any). In addition, the code within
each function should have comments as appropriate to explain how it
works.
- The program is well formatted (i.e. style). For example, you should
be consistent with your indenting in the program.