CSCI 261 - Programming Concepts - Spring 2022

Lab 4F - A Linked List Class Part IV: A Templated Linked List

This lab is due by Tuesday, March 29, 2022, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.


Instructions


Make a copy of your ending point from Lab4E as we finish modifying and building out our class.


The Constant Class


When working with the prior lab and the copy methods, you may have been tempted to mark the parameters as constant and your intuition is correct! We want to make sure the function doesn't inadvertently change the argument. However, whenever we tried to call a function from the constant object we would have been presented with an error.

  1. Go through your class declaration and mark any parameters that should not be allowed to change within the function
  2. Make sure you update the class definition as well
  3. Now go through and mark any methods that do not modify the callee as constant
  4. Make sure you update the class definition as well

Rerun your same tests you had in the prior lab. Nothing apparent should change, but we now have in place the proper protection to modify (or not modify) our objects appropriately.


The Templated Class


The Templated Node

In order to have the Linked List accept any type, we first need to have the Node struct accept any type. Rename Node.h to be Node.hpp to reflect that it is a templated type. Now add the templated typename so the value of the Node can be of the corresponding typename type.

The Templated Linked List

Refactor your LinkedList.h and LinkedList.cpp files into a single LinkedList.hpp file. Be sure to update the filename for including the Node.

Now go through the Linked List class and allow it to be templated. Since our data member will be a templated Node<T>, anytime we are working with a value it needs be of our template type. This would include:

Remember that the function defintions belong to the templated class type as well.

Now, update the include file in main.cpp and specify the Linked Lists operate on integers. Then edit your Makefile to no longer try to build LinkedList.cpp (it doesn't exist), build your program, and you'll see the same results from last lab. Hooray!

Leverage The Template

In main.cpp, we're only working with one type of Linked List. Add a second list of type strings. Perform the following operations on the string Linked List:

  1. Push front "is"
  2. Push back "working"
  3. Push front "This"
  4. Push back "now."

Then print out the contents of the list in order.

Now build. For any of the access functions, if you properly checked for null pointers or an in-range position then you may have hard coded in a return value of -1. When T is int, this works fine. However when T is string we have a type cast exception. The full error message is of the form:

error: no viable conversion from returned value of
type 'int' to function return type 'std::__1::basic_string<char>'

Reworded, an int cannot be directly cast to a string. This same problem will occur whenever T is not int (how do you convert -1 to be a Mug object we have custom defined?). Instead of hard coding in a return value of -1, we will instead want to return the default constructed object of the templated type. To return this value, we'll call the type's constructor with T().

(Aside: in practice and in the future, this is not the solution we'd ultimately use. For a string, T() returns an empty string "". For an int, T() returns zero 0. We have no way of knowing if that's the actual value at that position or the value signalling the position doesn't exist. Eventually we will want to move to being able to signal an error and not return any value. Stay tuned!)

Rebuild and rerun, everything runs smoothly once again!


Lab Submission


Submit your Node.hpp, LinkedList.hpp, main.cpp file(s).

You will submit your solution to this lab with the rest of Set4. Detailed instructions for doing this are posted in Assignment 4.


This lab is due by Tuesday, March 29, 2022, 11:59 PM.
As with all labs you may, and are encouraged, to pair program a solution to this lab. If you choose to pair program a solution, be sure that you individually understand how to generate the correct solution.