CS 215 - Fundamentals of Programming II

Spring 2006 - Programming Project 6

40 points

Out: March 24, 2006
Due: April 7, 2006 (Friday)


Consider a filing cabinet. Each cabinet consists of one or more drawers that contain zero or more folders in each drawer. We will assume that the folders contain information that can be arranged in some ascending order by an integer key value (e.g., ID number) and that they are stored in the cabinet in that order. To make folders easier to find, the maximum key value that may be contained in a drawer is marked on the front of the drawer and the drawers are ordered by their maximum key values. This implies that a folder is stored in the drawer with the smallest maximum key value larger than the folder key.


We can model such a filing cabinet as a (singly) linked list of drawers with each drawer containing a (singly) linked list of folders. The diagram below illustrates the structure (only) of the filing cabinet.


Project Specifications

The specifications for this project are as follows. There are three template structs and classes where the template parameter is the type of the information being kept in the folders: a folder list node (FolderNode<T>), a drawer list node (DrawerNode<T>), and the filing cabinet object itself (FilingCabinet<T>). For this project, we specify the maximum number of folders that will fit into a drawer, but we can add another drawer to a filing cabinet anywhere and at anytime.

FolderNode struct template

The FolderNode struct template is used to form a linked list of folders in a drawer. The user is required to provide an integer, called a key for each information object that is suitable for ordering the folders within the cabinet.

Attributes

Object Type Name
key value int key
information to store T item
link to next folder FolderNode<T>* next

Operations

There is just one operation other than direct access, the explicit-value constructor that creates a folder (node) by initializing the item, key, and link to the given arguments (or defaults).

DrawerNode struct template

The DrawerNode struct template is used to form a linked list of drawers in a filing cabinet.

Attributes

Object Type Name
maximum key value int maximumKey
number of folders in the drawer int numFolders
link to folder list FolderNode<T>* folderList
link to next drawer DrawerNode<T>* next

Operations

There is just one operation other than direct access, the explicit-value constructor creates an empty drawer (node) by initializing the number of folders to 0, the pointer to the folder list to null, and the maximum key value and the link to the given arguments (or defaults - the constant INT_MAX is defined in library <climits>).

FilingCabinet class template

The FilingCabinet class template ties everything together.

Attributes

Object Type Name
link to first drawer DrawerNode<T>* drawerList
maximum number of folders per drawer int maxFolders
total number of folders in cabinet int totalFolders

Operations

Default constructor - creates an empty filing cabinet by initializing totalFolders to 0 and maxFolders to 25, and creating one (empty) drawer initialized with a maximum key of INT_MAX.


Explicit-value constructor - creates a filing cabinet from a vector of items whose keys are in a corresponding vector of integers, and also initializes the maximum number of folders per drawer and the maximum key value to the given arguments (or defaults). Each item is put into a folder (node) that is then put in a drawer (node).


Copy constructor - creates a new FilingCabinet object that is a copy of an existing FilingCabinet object.


Destructor - deallocates the folder and drawer nodes.


operator= - overloaded assignment operator function. Copies original FilingCabinet object into an existing FilingCabinet object.


AddFolder - inserts the given item with the given key in order into the drawer with the smallest maximum key value larger than the given key, and increments the number of folders in the drawer it is placed in and the total number of folders. If the given key is already in the cabinet, throw a DuplicateError with an appropriate message (see below regarding exception objects). If the given key is larger than largest maximum key value, then throw a RangeError. If the drawer is full (i.e., it holds the maximum number of folders), insert a new drawer and put half of the folder list of the original drawer into it, appropriately setting the number of folders and the maximum key value, then proceed to add the new item to the appropriate drawer.


RemoveFolder - removes the item with the given key, decrements total number of folders and the number of folders in the drawer it is removed from. If the key is not in the cabinet, throw a NotFoundError with an appropriate message (see below regarding exception objects). Note that this may result in an empty drawer, which is allowable.


GetFolder - returns the item (not the node) with the given key. Returns a reference to allow item to be manipulated directly. This version is non-constant. If the key is not in the cabinet, throw a NotFoundError with an appropriate message (see below regarding exception objects).


GetFolder - constant version with the same analysis as the non-constant version above. If the key is not in the cabinet, throw a NotFoundError with an appropriate message (see below regarding exception objects).


GetTotalFolders - returns total number of folders in the cabinet.


ListFolders - output the item in each folder from each drawer using operator« separated by the given separator string (or default). The drawers should be numbered consecutively starting with 1 and the number of folders and maximum key in each drawer should be output as well. The output for the above diagram might look like:

Drawer 1: 3 folder(s), max key is 250
<item information for 123>
<item information for 178>
<item information for 250>
Drawer 2: 0 folder(s), max key is 428
Drawer 3: 1 folder(s), max key is 637
<item information for 543>
Drawer 4: ...
:
:
Drawer n: 5 folder(s), max key is 999
<item information for 834>
:
:
<item information for 987>


FindDrawer - a private helper member function that returns a pointer to the drawer that might contain the folder with the given key. Returns null if the key is larger than the largest maximum key value. Must be used by AddFolder, RemoveFolder, and GetFolder.

Assignment

Implement the three template structs and classes as shown above. This project must be implemented using linked lists. Projects that do not use linked lists will be returned for resubmission with late penalty.


Put the implementations for all three structs and classes into one file cabinet.h in the order presented above. (This is because you cannot really use the node structs independent of the filing cabinet class so you might as well put them in the same file.) For the struct templates, the constructor implementation should be written in-line in the struct definition as shown in class. The FilingCabinet operation implementations should be separate template function definitions. If you wish to write template functions for list operations such as CopyFolderList or DeleteFolderList, put them in this file as well. (Use comment blocks to identify the different sections of the file.)


A driver program (cabinetdriver.cpp), a sample data file (students.dat), and a new except.h that defines the DuplicateError and NotFoundError exception objects will be made available by Wednesday, March 28, in directory /home/hwang/cs215/project6 on csserver. This program will define a StudentInfo struct containing a name and student ID and create several filing cabinets of these structs. The student ID will be used as the folder key.


You must submit a makefile for this project that compiles the driver program even though there will be only one object file target (cabinetdriver.o). Submissions without working makefiles will be assessed up to 3-point penalty as indicated in the syllabus.


Follow the guidelines in the C++ Programming Style Guideline for CS 215 handout. As stated in the syllabus, part of the grade on a programming project depends on how well you adhere to the guidelines. The grader will look at your code listing and grade it according to the guidelines.


Submit a tar file with your makefile and cabinet.h. Turn in a hardcopy of your makefile and cabinet.h. Please do not submit the driver program, the sample input file, object files, or executable files (either in the tarfile or in hardcopy).


REMINDER: Your project must compile for it to be graded. Submissions that do not compile will be returned for resubmission and assessed a late penalty.



Converted using latex2html on Thu Mar 23 21:00:14 CST 2006
Figure added manually