CS 311 Fall 2024 > Assignment 6
CS 311 Fall 2024
Assignment 6
Assignment 6 is due at 5 pm Thursday, November 7. It is worth 65 points.
Procedures
This is, optionally, a group assignment. You may work in a group of two, if you wish. Each group only needs to turn in a single copy of the assignment. Under normal circumstances, each group member will receive the same grade on the assignment.
Turn in answers to the exercises below on the UA Canvas site, under Assignment 6 for this class.
- Your answers should consist of
the file
da6.hpp
, from Exercises A and B. Attach this file to your homework submission. - I may not look at your homework submission immediately. If you have questions, e-mail me.
If you work in a group:
- The assignment files must contain the names of both group members.
- One student submits the assignment as usual, on Canvas.
- The other student submits an empty assignment (no attached source or header files) on Canvas, with a note indicating whom the assignment was done with.
Exercises (65 pts total)
General
In each of the following exercises,
you are to write a function template or class template.
All code must be in file da6.hpp
.
Since everything you write will be a template,
there will be no source file.
Each exercise will make use of the Linked List in file
llnode2.hpp
(in the Git repository).
Do not duplicate the code in this file;
instead, do “#include "llnode2.hpp"
”.
And do not modify file llnode2.hpp
.
Your code must reference Linked List nodes via unique_ptr
.
All creation of nodes must be done via make_unique
,
or using function push_front
in
llnode2.hpp
.
You may not use new
or delete
.
You may use any C++ Standard Library functionality you wish.
Be sure to follow the coding standards. All standards now apply!
Test Program
A single test program for all of the exercises
is available:
da6_test.cpp
.
If you compile and run the test program (unmodified!) with your code,
then it will test
whether your code works properly.
Note that your code will not compile with the test program unless all required functions and classes exist. Therefore, you must write dummy versions of all functions—at least—or your work will not be graded.
The test program requires doctest.h
,
the header for the doctest unit-testing framework,
version 2.
Do not turn in the test program,
file llnode2.hpp
,
or the doctest framework.
Exercise A — Reversing a Linked List
Purpose
In this exercise, you will write an efficient reversing function for a Linked List.
You will make use of smart-pointers.
Instructions
Write a function template reverseList
,
prototyped as follows.
[C++]
template<typename ValType> void reverseList(unique_ptr<LLNode2<ValType>> & head);
This function is given a unique_ptr
to a Linked List—an empty unique_ptr
if the list is empty.
It alters the list so that the values appear in reverse order.
A pointer to the new list is returned via the reference parameter.
Function reverseList
must meet the following requirements.
- It performs no value type operations at all.
- It is in-place.
- It runs in linear time.
Exercise B — Associative Dataset Class Template
Purpose
In this exercise, you will write a class template that uses a Linked List to hold an associative dataset. This is not a terribly good way to implement such a dataset; we will cover better ways later in the semester.
As in the previous exercise, you will also make use of smart-pointers. And you will signal errors by throwing exceptions.
Background
An associative dataset is one in which items are looked up by key. Often there is a value associated with each key; in this case the dataset consists of a number of key-value pairs.
Instructions
Implement a C++ class template that stored and manages an associative dataset consisting of key-value pairs. The types of the keys (the key type) and associated values (the data type) should be specified by the client.
Duplicate keys should not be allowed.
Determining whether two keys are the same should
be done using the equality (==
) operator.
- Call your class template “
SLLMap
”, so that, for example, anSLLMap
with key typestring
and data typeint
would be declared asSLLMap<string, int>
. - The class must have one and only one data member,
which is of type
unique_ptr<LLNode2<
KVTYPE>>
, where KVTYPE is a type that can hold a single key-value pair (e.g., an appropriatestd::pair
orstruct
). The stored associative dataset must be held in a Singly Linked List of key-value pairs, with this data member as its head pointer. - Member Functions.
SLLMap
must have the following public member functions, and no others:- Default ctor. Creates an empty dataset.
- Dctor. As usual.
- Function
size
. No parameters. Returns an integer of an appropriate type giving the number of key-value pairs in the dataset. - Function
empty
. No parameters. Returnsbool
. The return value istrue
if there are no key-value pairs in the stored dataset, andfalse
otherwise. - Function
present
. One parameter: a key. Returnsbool
. The return value istrue
if a key equal to that given lies in the stored dataset, andfalse
otherwise. - Function
get
. One parameter: a key. Returnsconst
DATA_TYPE&
for a constSLLMap
andDATA_TYPE
&
for a non-constSLLMap
. If an equal key lies in the stored dataset, then the associated value is returned as indicated. Otherwise, an exception of typestd::out_of_range
is thrown, with thewhat
member set to some appropriate human-readable string. - Function
set
. Two parameters: a key and an associated value. Returns nothing. If an equal key does not lie in the dataset, then the key-value pair is inserted. If an equal key does lie in the dataset, then the existing key-value pair is replaced with that given. - Function
erase
. One parameter: a key. Returns nothing. If an equal key lies in the dataset, then that key-value pair is removed. If an equal key does not lie in the dataset, then the function does nothing. - Function
traverse
. One parameter: a function or function object (its type can simply be a template parameter). Returns nothing. The passed function is expected to take two parameters, key type and data type, and return nothing. The passed function is called on each key-value pair in the dataset.
- Again,
SLLMap
must not have any other public member functions. In particular, all of the following must be deleted.- Copy ctor.
- Move ctor.
- Copy assignment operator.
- Move assignment operator.
- All member functions should have the highest reasonable levels of exception safety.
- All member functions must be exception-neutral; that is, exceptions thrown by value-type operations must propagate unchanged to the caller.
- You may not forbid any member function of the key type or data type from throwing, except for the destructors.
It is not expected that SLLMap
will be terribly efficient.
Indeed, the requirements of Exercise B make high efficiency
impossible.
And much of the rest of the semester will be devoted
to how we might do better.
But you should write reasonable code,
which is not unduly inefficient
within the requirements of this exercise.