The goal of this project is to extend the GenList class to allow the client to use a linked list much like an array. The premise is that we can combine the simplicity of arrays with the efficiency of linked lists. For example, we want the client to be able to refer to an item in a linked list using an integer index. Thus,
Extending the GenList class as described above is fairly straightforward --- we simply have to overload the [] operator and add a few new member functions. The ith item of a linked list can be accessed simply by traversing the linked list from the beginning and counting up to i. So, of course, there must be a twist to this project. Consider what would happen if the client simply wanted to print out each item of the list using a for loop and the [] operator. Each time the client accesses the linked list, we would have to start from the beginning of the linked list to find the ith item. This is horribly inefficient because it uses O(n2) time for a process that should take O(n) time.
One way to improve the performance of these operations is to use a caching strategy. Whenever we access the ith item in the linked list, we can "remember" the index i and a pointer associated with the ith item. These would be stored as data members of the class derived from GenList. If the client accesses the (i+1)th item in the next operation, we can find this item starting from the ith item rather than the beginning of the linked list. This saves a lot of time if the linked list is long. For several reasons, it is actually more convenient to store the pointer to the (i-1)th item instead of the pointer to the ith item. For example, if the client wants to delete the ith item right after finding it, then we need the pointer to the (i-1)th item to accomplish this without starting from the beginning of the array.
A big problem with this caching scheme (and with caching schemes in general) is that the cached values may become invalid. For example, if we access the ith item in the linked list and immediately insert a new item in the beginning of the linked list, then the pointer we cached is no longer valid as the pointer associated with the ith item. Fortunately, all the functions which modify the linked list in such a way also set the data member cache_status to dirty. The indicates that the cached values are no longer valid and we must start at the beginning to find the ith item.
This caching strategy doesn't solve all of our problems with efficiency. If the user wants to access items in the linked list in reverse order or in some random order, then these operations would be slow compared to an array. However, it can be argued that many, if not most, of the operations can be improved significantly using this caching scheme. There are many possible variations to the caching scheme we use here. For example, we can cache several pointers instead of just one. We will not pursue these variations in this project. However, it should be noted that in many areas of computer science --- including computer architecture, networks and operating systems --- developing a sophisticated and efficient caching strategy is of critical importance.
Your assignment is to extend the GenList class using the caching strategy described above. Your extension should include the following:
Each of the 4 functions above must implement the caching strategy. Your derived linked list class must remain generic. I.e., it should still be possible to insert integers and strings into the same linked list. For testing purposes, you must create a publicly derived class from ListCell called StudentRecord that holds the following data:
For the StudentRecord class, you must implement all the virtual functions in the ListCell class. This is in addition to the constructors and destructors.
These are provided to you so you can work on this project on your own computers. You should not make any changes to either the declaration or the implementation of these classes. All new features should be put into the derived classes. If you think there is an error in the implementation of the base classes, contact the instructor.
Turn in all the files that are needed to compile your program, except for genlist.h and genlist.C. Your programs should work without any modifications to these files. In addition, you should turn in a main program that sufficiently demonstrates the caching features and the genericness of your programs. Include a typescript file and a README file for the graders.
The Perl programming language is used extensively for scripting web sites. One of the very convenient features of Perl is called associative arrays. An associative array essentially allows you to index an "array" using a string instead of an integer index. In our project, you would be able to obtain the StudentRecord for a student named "John Smith" using the syntax:
where L is a linked list. If the linked list does not contain a StudentRecord of a student named "John Smith", then a new item is added to the linked list with that name. How should caching be handled with this feature? Using the same string index twice should not result in a search through the linked list twice.
For an extra 10%, implement associative arrays for your list class including an efficient caching scheme. The asociative array feature does not have to work with derivations of ListCell other than StudentRecord. As usual, extra credit is all or nothing --- you either get 10% or 0% extra credit.