You can skip the append(), unless your teacher requests it.
There must be a lot of material about
linked lists.
Your code had:
cout << headPtr->getItem()<<endl;
How would that look in a function?
Perhaps:
1 2 3 4 5
|
template<typename T>
void print( const Node<T>* node )
{
std::cout << node->getItem() << '\n';
}
|
Do you see a danger in that? What if I call
print( nullptr );
?
How to cope with that?
Furthermore, the function prints (at most) one node's value. You want whole list.
Iteration, loop, recursion.
Lets look at a loop:
1 2 3 4 5 6
|
std::list<Bar> foo;
auto gaz = foo.begin();
while ( gaz != foo.end() ) {
// use gaz
++gaz;
}
|
The
gaz
is this is an
iterator. Probably a sugar-coated pointer.
It refers to an element of foo. The
++gaz
updates the gaz to refer to the
next element.
If gaz is at the last element, then ++gaz makes it equal the foo.end().
If you have a pointer to node of your list, how would you get to the next node?
How could you see whether you point one past the last node?
(You already have code that holds the answers. Now you have to
see them too.)