(Latest Revision: 04/11/2009)
Lists: Implementing a Circular List
PRELIMINARIES:
Before starting this assignment you have to be familiar with chapter 4 of
Carrano, including the section entitled "Variations of the Linked List." You
also need to read the directions and examples here:
http://www.cs.csustan.edu/~john/Classes/General_Info/progAsgRules/
THE ASSIGNMENT:
- Write a driver for a list-of-strings class.
- Implement a new list class by using the files
cListP.h
and
cListP.cpp
after
"filling in blanks" in
cListP.cpp.
The implementation:
- must be a singly-linked circular list
structure,
- must employ an external pointer to the last node,
- must use a NULL value in the external pointer to
indicate an empty list,
- must not employ any dummy nodes,
- must employ dynamic memory allocation, and
- must be efficient.
- Do testing to show that your cListP.h, cListP.cpp, and
driver work together properly.
A circular linked list is a linked list in which the last element
points back to the first element, instead of having a NULL
pointer. You can get a lot of ideas on how to write the code for
the new circular list class by looking at the "regular" linked
list implementation code:
ListP.h
and
ListP.cpp.
You must be careful to understand where and how the new "cListP" code must be
different from the "ListP" code.
THE DRIVER:
Before you implement the new list class, you must write a simple driver
program. The purpose of the driver is to process an arbitrary sequence of
commands -- commands to perform operations on a list of strings.
The driver must:
- create a list variable, and
- fetch, echo, interpret, execute, and tell the results of
each command in the input,
If we take a closer look at the job of the driver, we see it must
execute a "command loop." In each pass through the loop, the
program reads a one-line command from standard input, interprets
the meaning of the command, echos the command to the standard
output, executes the actions required by the command, and tells
the results of the command. (The print command mostly just prints
and does little additional "telling.")
The command loop must be written so that when it receives the
stop command it prints a goodbye message and then
exits.
It should not be a problem to develop the driver before the new
list class is finished. For example, you can just put stub code
in the functions of
cListP.cpp
and then test run the driver. When the driver executes, it will
call the stub functions in cListP.cpp. Alternatively, you can
make a minor modification in a "#include" directive in the
driver, re-compile, and then test the driver using the "ListP"
implementation of the list class. When programs are written in a
modular fashion, this kind of thing is very easy to do. It's
like plugging your headphones into one MP3 player, and then
another -- pretty simple and easy to do.
Implement the "print" command completely within the driver. Make it a
function inside the driver that uses the ListRetrieve function of the list
class to retrieve and print each element of the list.
DRIVER INPUT AND OUTPUT:
The program must read all its input from standard input, and write all its
output to standard output. (Naturally it will be convenient if you prepare
files of input in advance and use unix redirection to allow the program to
read its input from a file instead of the keyboard.)
The document
sample.in
is an example of the type of input the driver must process. This file:
sample.out
shows the corresponding output.
Most inputs are simple commands that tell the driver to perform operations on
the list. There is also a stop command that tells the program when
to quit.
The sample input set is not intended to test the program exhaustively. You
need to do part of the work of designing and making the test inputs.
Figure out what data boundaries and what average data examples should be
tested for this program. When you make your test script, run tests on files
with the appropriate content.
IMPLEMENTING THE NEW LIST CLASS:
You must create a new list class similar to the "ListP"
singly-linked version of the list class we studied in chapter
four of the Carrano text book. You must start with copies of the
cListP.h
and
cListP.cpp
files in this directory and fill in the missing function bodies in
cListP.cpp
as required to make the list class operational. (There will be no
need to change anything in the cListP.h file - I don't want you to.)
You must write the function bodies so that the functions are
implemented exactly as specified by the function
prototypes and the the function header comments in
cListP.h.
Also, you must make sure the operations are adequately efficient.
The work done by PtrTo, ~listClass, and
Print should be bounded by a constant times the number
of elements in the list. There is no way to do better than that,
and the code should not do worse than that.
ListRetrieve, ListInsert, and
ListDelete will need to call PtrTo. Besides
the work done by PtrTo, the rest of the work these
functions do should be bounded by a constant.
The amount of work done by listClass,
ListIsEmpty, and ListLength must be bounded by
a constant.
COMPILING:
Assuming that your class implementation file is named cListP.cpp
and the file containing your driver is named driver.cpp, you
should compile the program like this:
g++ cListP.cpp driver.cpp
include cListP.h in cListP.cpp and driver.cpp. (Do not
include cListP.cpp in driver.cpp or vice-versa.)
WHAT TO TURN IN:
You will turn in two hardcopy versions of your program and you will
send me two e-mail messages.
With regard to the e-mail please follow these rules:
- Always send me e-mail as plain text in the main message
body. Never send me attachments.
- Always use the exact subject line I specify for each
message. (I often get hundreds of e-mail messages in a week.
The subject line allows me to find, filter and sort messages.)
You will lose a significant number of points on the assignment if
you use the wrong subject line.
- Be very careful when typing the command to send e-mail. You may
use the instructions in your
Hello World! lab excercise
for guidance. Of course, you will need to make the obvious
changes to those directions -- you have to use the correct
subject line and filename.
- Always send yourself a copy of each e-mail message you send to
me, and check immediately to see if you receive the
message intact. You are responsible for sending e-mail
correctly.
Here is a list of the things you have to turn in:
- At the start of class on the first due
date, turn in a hardcopy of your completed driver
code.
- Send a copy of your completed driver code to me by e-mail before
11:59PM on the first due
date. Use this subject line:
CS2500Prog4_DRV
- At the start of class on the second due
date, turn in hardcopies of
- your completed driver code,
- the cListP.h file, and
- your completed cListP.cpp file
- Send the following items to me by e-mail before 11:59PM on the
second due date:
- your completed driver code,
- the cListP.h file, and
- your completed cListP.cpp file, and
- a script showing your test runs.
Combine all the source files and the script into one
shell archive file
and e-mail me the archive file with the subject line:
CS2500Prog4.FIN
Note that there are no spaces in the subject lines given. It is
important that you do not insert any spaces. My e-mail address
is:
john@ishi.csustan.edu.
DUE DATES:
For the due dates, see
the class schedule.