CS 368 Program 2: SortedList Class
What to Write
Complete the SortedList class partially defined below
and available to copy from
~cs368-2/public/SECTION2/turnin/PROGRAM2/SortedList.h
by writing all of the member functions, as well as a main function as follows:
- The constructor function: Initialize the list to be empty.
- Insert: Add the given string to the list in its proper place
(so that the list is still in alphabetical order).
If the string is already in the list, it can be added
either before or after the duplicate(s) that are already
in the list.
- Print: Print (to the
given ostream) a left square bracket followed by each string in
the list, separated by spaces, followed by a right square bracket.
(If the list is empty, just print "[ ]".) For example, if the list
contains the strings "goodbye" and "hello", the Print function
should print: "[ goodbye hello ]". Please follow the exact formatting
given here:
If the list has strings, the output should be laid out as below:
[
space
string_1
space
string_2 ...
space
string_n
space
]
If the list is empty, the output should be laid out as below:
[
space
]
space represents one ' ' (space).
- Lookup: Return the number of copies of the given string that
are in the list.
- main: This function will be written
to expect two command-line arguments (the names of two files).
If one or both arguments are missing, or the files cannot be
opened for reading, the program should give an error message and quit.
Otherwise,
it should read strings from the first file into a list; then, for
each string in the second file it should write (to the
standard output) the string itself, and the number of times that
string occurred in the first file (one string/number pair per line).
You should assume that
both files contain only lower-case words (no punctuation).
Note that the running time of your constructor function should be constant,
and the running times of your Print, Insert, and Lookup functions should be
proportional to the number of strings in the list.
Also, note that only main and Print create any output. No other methods
should output any information.
What to Hand In
Put your definition of the SortedList class in a file called SortedList.h.
Put the code for the SortedList member functions in a file called SortedList.C.
Put your main function in a file called main.C.
(Note that you will need to #include "SortedList.h" in both
SortedList.C and main.C, and that to create an executable you will
need to use: g++ SortedList.C main.C)
Hand in your code by uploading the three files.
Grading Criteria
Your grade will depend on:
- how well you follow the "What to Hand In" instructions
- the correctness and efficiency of your code
- your coding style (i.e., is it easy to read and understand, does
it reflect a good understanding of basic C++ ideas)
Note that we will be writing our own main functions to test your SortedList
code, so it is very important that you use the function names and the
files names specified in this assignment.
Also, in your SortedList.C file, please include comments that set off each
function. For example:
/*
* Insert
*/
... code for the Insert function...
The SortedList Class
#include <string>
#include <fstream>
/*
* SortedList class
*
* A SortedList is a collection of strings in alphabetical order
*
* Operations:
* constructor Initialize the list to be empty.
* Insert Add a given string to the list.
* Print Print (to the given ostream) the strings in the list in
* order, enclosed in square brackets, separated by spaces.
* Lookup Return the number of times a given string occurs in the list.
*/
class SortedList {
public:
// constructor
SortedList();
// modifiers
void Insert(string s);
// other operations
int Lookup(string s) const;
void Print(ostream &output) const;
private:
struct ListNode {
string data;
ListNode *next;
};
// pointer to the first node of the list
ListNode *head;
};
What to turn-in:
|
SortedList.C
SortedList.h
main.C
|