C++ 11 has been here for awhile ... so it's time to 'open the books' and start to use some of the nice coding features like
auto and
lamda's ...
Also, time to tackle the tricky code needed to add
const_iterator to a simple single linked-list ... a list that already works pretty well with just an iterator ...
The approach taken here, will be as follows:
Firstly, let's look at a very simple SLList, and see if we can get that working. It will be just two files, a test .cpp file and a template class list .h file
Here we will use 'nested classes' ...all the List code is in the one .h file.
The list here is pretty 'bare bones' ... but that's ok ... because we just want to see the code for the nested iterator and const_iterator classes.
Note! This first example will be done using a C++11 compiler.Secondly, we will take ... what I recently read ... was a 'more STL like approach' and use separate files for the parts needed ... 1) Node, 2) iterator, 3) const_iterator, 4) SLList ... and 5) a little test file
Note the use of typedef here to 'import/nest' the iterator and const_iterator classes 'inside' the SLList class.
Also note the frequent use of friend classes and forward declarations of these classes before they are defined, to facilitate them being 'friends' ...
Note! This second example will be done using a PRE-C++11 compiler ... (i.e. ISO C++)
The third example, will be done using a C++11 compiler ...Just a short test file,
and two .h files ...
one for the node class
and
one for the single linked list class
This single linked list is a little more 'embellished' ...
for example it demos/uses move copy and move assignment
The methods defined here are mostly just the ones you might want to have ... i.e. ... if you wanted to derive a STACK class from this single linked list class ... which is also shown next ...
And then ... a little demo program, that uses that (derived) STACK class ... to solve a palindrome type student problem.
Note here, the iterator and const_iterator classes are 'nested' in side the single linked list class.
I would suggest that you follow this development sequence ...
from an easy 'bare-bones' list ...
to a little more functional single linked list ...
because we want to focus here,
not on all the functions,
but rather on how to get the const_iterator parts merged in the list ...
It may take a while for you to 'see' the several key coding changes/additions ...
to make the transition for just having an iterator ...
to ALSO having a const_iteratoer ... in your list class.
It took me much 'digging' and 'thinking' and 'trying' ... to finally get it (all?) to work ...the way I 'expected' it should work.
Enjoy ... but be prepared for taking some time to dig in ... read all the comments ... make some code 'changes/edits/errors' ... and read the error messages ... and add some tests and see the code working (or not?)
Shalom shalom,
David