Programming Assignment #1
CS163: Data Structures
Scope:
When beginning with this project, the
first thing to keep in mind is that we are no longer working on our CS162
project! For each assignment we only have approximately 2 weeks to complete the
assignment. Therefore, it is critical that you focus on a limited scope
to each program. You are not required to do the level of error checking (on
data entered from the user) performed in CS162. You are also not required to
focus heavily on the user interface. Instead, focus on how to design classes
that are well structured and efficient and on the required data structures. We
will concentrate on directing you in the lectures on how best to create your classes.
Focus as well on your dynamic memory (allocation, traversal, and deallocation).
We will be grading your projects from
the standpoint of how well your classes perform, how well you use dynamic
memory, and not on how elegant your user interface is! Of course, your user
interface must be clear enough for us to test your program.
This first program of the term is an
exercise in building, traversing, and destroying linear linked lists. Think of
it as a “proof of concept” program, rather than building a complete
application!
Program Statement:
Design and implement a C++ program using data abstraction to
create an Abstract Data Type called a self
adjusting list for arrays of characters. The goal of the user will be to
enter and remove text in a paragraph. As text is added and removed the amount
of dynamic memory used will grow and shrink.
The self
adjusting list stores character
arrays in a linear linked list of arrays (where each array will be at most 10
characters). A self adjusting list can
grow and shrink to closely match the actual memory requirements for the array
without requiring the entire memory to be reallocated and the data to be
copied. Therefore, if we first store the word “CS163” into the array, only 6
characters (one for ‘\0’) are needed. When we add the words “ is really fun”,
an additional 2 nodes will be added (one for the next 10 characters, and
another for the remaining).
The primary operations that will be used
by the client are the ability to adding
(concatenating) and remove words from
the self adjusting list, while at the
same time trying to minimize the use of memory and number of times things are
copied. Suggested class interface:
class list
{
public:
list();
~list();
int
append_word(char word[]);
int
remove_word(char word[]);
int
display_all();
private:
node
* head;
node
* tail; //suggested for efficiency
};
Avoid
Using:
1)
Do
not use statically allocated arrays in your classes or structures. All memory
must be dynamically allocated and kept to a minimum!
2)
All
data members in a class must be private
3)
Never
perform input operations from your class in CS163
4)
Global
variables are not allowed in CS163
5) Do
not use the String class! (use arrays of characters!)
Hints
on the Client Program:
Write a client program (i.e., main
program) that will read input from the keyboard and use class member functions
to perform the necessary operations. I suggest writing a menu so that user (and
grader) can easily test out the ADT operations. However, this is not like the CS162 project; you do not need to have error
checking to the degree that we implemented in CS162. You can assume that the
user will enter valid data.
Things you should know...as part of your
program:
1) Use modular design, separating the .h
files from the .cpp files. Remember, .h files should contain the class header
and any necessary prototypes. The .cpp files should contain function
definitions. You must have at least 1 .h file and 2 .cpp files. Never "#include" .cpp files!
2) Use the iostream.h library for all I/O;
do not use stdio.h.
3) Make sure to define a constructor and
destructor for your class. Your destructor must deallocate all
dynamically allocated memory.
4) Each node and each array
within the node must be dynamically allocated to be of the appropriate size. Do not use statically allocated
arrays in your nodes! All names should be implemented using arrays of
characters.
What
to Turn in:
• On the due date, email only
your source code to: karlafgr@cs.pdx.edu
• Make sure to use the word “CS163 submission”
in the subject of your email
• Remember that 20% of each program's grade is based on a written discussion of the design. Take a look at the style sheet which gives instruction on the topics that your writeup needs to cover.