February 25, 2002
Reading assignment:
Deitel and Deitel - 15.2-15.4
-or-
Other texts: linked lists
Today we'll work on programming assignment 4 together again
1. Dynamically allocated memory - I suggest that you have the
StudentGroup class allocate all memory for StudentRec, and
handle de-allocating it as well. The allocation will take place
when a student is added (so StudentGroup::insert should take
the student data as a parameter, and can allocate the StudentRec
as soon as it finishes validating the data). This will happen multiple
times when a database file is read. The memory will be deallocated
when a student is deleted (StudentGroup::delete can do this) or
in the destructor for StudentGroup, which will deallocate all
StudentRecs in the entire list. Under this plan, the destructor
for StudentRec will do nothing, except possibly set its next pointer
to 0.
2. Which class does which tasks? The StudentRec class represents
the data for a single student - this class doesn't have to do
anything other than manage the data for a single student. The
StudentGroup class, on the other hand, is responsible for managing
lists, so all of the linked list algorithms will be found in
StudentGroup. The main program is responsible for creating variables
to hold the lists themselves (in other words, it creates 4
StudentGroup objects - it can make an array of them if you like).
It's also responsible for keeping the menu, managing input from the
user, reading and writing files, and interfacing to the appropriate
StudentGroup member functions.
3. The linked-list algorithms (these go in the StudentGroup implementation)
The easiest one is StudentGroup::display
make a pointer to a StudentRec - this pointer will traverse the list
I'm going to call it cptr for now - it points to the StudentRec
that I'm about to display
start off by making cptr point to the listhead
make a loop that runs until cptr is 0 (end of list)
display the current record (call the StudentRec display
function for the StudentRec that cptr is pointing to)
update cptr so it points to the next record
4. Another linked-list algorithm - search by id
Let's say this function takes a string * - the id that we want to match
We traverse the list as we did when we displayed, but instead of
displaying, we check to see if the id matches the id of the current
student we're pointing to. If it is, yeay, we found it, otherwise
continue on through the list. This function should return a pointer
to the matching StudentRec, or 0 if it wasn't found
make a pointer to a StudentRec - this pointer will traverse the list
I'm going to call it cptr for now.
start off by making cptr point to the listhead
make a loop that runs until cptr is 0 (end of list)
compare the id passed as a parameter to the id of the StudentRec
that cptr points to
if they're the same, then return cptr
otherwise, update cptr so it points to the next record
If you finish the loop without finding it, then return 0
5. Another linked-list algorithm - add student
I suggest that you start by having this function just insert the student
into the beginning of the list. You'll have to change this later, but
you'll have more experience by that point. This function should take
first name, last name, id, gpa and year of the student. It should
return a boolean, true if it added the student, false if the student
id was a duplicate.
First, traverse the list searching for a match on the id. This
requires a loop like we've seen twice before. If we find a match,
then return false.
If we made it this far, then dynamically allocate a new
StudentRec, initializing with the parameters passed.
Make the next pointer for this new student equal to the old listhead
Make the listhead point to the new student we just created.
update the size of the list
return true