Exercise 36
Info

This is an in-class exercise. An exercise page like this one will contain a brief description but is intended to be supplemented by discussion during our meeting time. Complete the exercise to the best of your ability in the time given. Feel free to talk with other students as you work, and do not be afraid to ask questions if you get stuck. Aim to complete as much as possible during our meeting. You are encouraged to work at home to complete what you do not get through today, and ask questions over Piazza or in office hours.

Learning Objectives

Gain practice writing an iterator and const_iterator for a container class.

Part 1

Pull the starter code for this exercise from the public repo by taking the following steps:

  1. Log into ugrad, then navigate to the public repo and type git pull to synchronize your local working copy with the remote repo.

  2. Copy the above files from the public repository (in the /exercises/ex37/ directory) into your personal repo where desired. Then navigate to your personal repo folder for this exercise. Confirm that you can see the template files for today’s exercise – files named Makefile, MyNode.h, MyList.h, MyList.inc, and main1.cpp, main2.cpp, main3.cpp, and main4.cpp inside.

Part 2

Open MyList.h, MyList.inc and MyNode.h to see how they are implemented. Compile and run the code in main1.cpp using the commands make main1 and ./main1. Check the output of the program and confirm that it makes sense, given the source code. Do you understand why it is possible for main() to modify the list via the pointer returned by mli.get_head()?

Part 3

Read through main2.cpp. You can try to compile the code using the command make main2 however you will receive a bunch of errors because the MyList<T>::iterator nested class is not fully defined in MyList.h (see TODO Part 3a). Following the comments in MyList.h, fill in the missing portions of MyList<T>::iterator until main2.cpp compiles and prints the expected output.

Once you have that working, see TODO Part 3b in main2.cpp. When you uncomment the line just below, the code should still compile, and the output should show that the list elements were changed.

Part 4

Read through main3.cpp. You can try to compile the code using the command make main3 however you will receive a bunch of errors because the MyList<T>::const_iterator nested class is not defined (see TODO Part 4a in MyList.h). Fill in the missing portions of MyList<T>::const_iterator until main3.cpp compiles and prints the expected output.

Once you have that working, see TODO Part 4b in main3.cpp. When you uncomment the line just below, the code should fail to compile, since the const_iterator should not allow you to modify elements of the list.

Part 5

Read through main4.cpp. You can try to compile the code using the command make main4, however you will receive a bunch of errors because the MyList<T> constructor that takes two iterators (see TODO Part 5 in MyList.h) is missing. Fill in the missing portions of template<typename Itr> MyList<T>::MyList<T>(Itr i_begin, Itr i_end) until main4.cpp compiles and prints the expected output. Note that the typename Itr is just another placeholder like our usual typename T, but here Itr will stand for some iterator type.

Reminder

Remember to add and commit to your local repo copy as your work. Push to your remote repo when finished and submit to Gradescope to check your solution. Use exit to logout from your ugrad account when finished. If you continue to work on the program after class, make sure to keep your repo updated as well!