void List::insert_at_back(int value)for inserting values to the end of the list. Modify the test program to test your changes.
int List::remove_from_front()for removing values from the start of the list. Modify the test program to test your changes.
int List::remove_from_back()for removing values from the end of the list. Modify the test program to test your changes.
unsigned long List::count() constwhich counts how many items are in the list by traversing it. Modify the test program to test your changes.
unsigned long List::size() constwhich returns how many items are in the list by storing an unsigned long variable in each List object and updating this variable every time a method causes the size of the list to change. Modify the test program to test your changes.
ConstIterator(const Iterator &i);which converts a normal Iterator into a ConstIterator.
This will require the Iterator class to define the ConstIterator class as a friend. This requires a forward definition which tells the compiler that the ConstIterator class hasn't been defined yet, but it will be later.
// Forward definition, tell the compiler that ConstIterator exists // but hasn't been defined yet. class ConstIterator; class Iterator { // This line requires the above forward definition friend class ConstIterator; // rest of Iterator class goes here }; class ConstIterator { // ConstIterator class goes here };
Iterator begin(); Iterator end(); ConstIterator begin(); ConstIterator end();then the compiler gives an error. This is because you cannot overload functions based on their return type (can you think of why?). However, if you do
Iterator begin(); Iterator end(); ConstIterator begin() const; ConstIterator end() const;then the compiler accepts the overloaded methods, and so it is possible to provide const and non-const versions of methods where it's appropriate.
void output_list(const List &list);This is more correct, since output_list() shouldn't be allowed to modify the list being output.
However, notice now that you cannot get a non-const Iterator using a const List reference - the compiler complains that using the "begin()" and "end()" methods would ignore the "const" in "const List &". Modify the code for output_list() so that it uses a ConstIterator rather than an Iterator. Notice that now the "begin() const" and "end() const" methods are being used, which is allowed with the "const List &list".
static Iterator Iterator::create_begin(const List &l);which returns an Iterator which is at the start of the given List (don't use the List::front() method). Then create a similar static method
static Iterator Iterator::create_end(const List &l);which returns an Iterator which is at the end of the given List (don't use the List::back() method). You may need to declare the Iterator class as being a friend of the List class. The methods Iterator::create_begin() and Iterator::create_end() are in some sense "pseudo-constructors", and the trick of using static methods to create objects with particular properties in this way is called the Named Constructor Idiom.
Use this small test program to observe how changing the type of iterator can affect the actual iteration through the list.