My professor has asks us to reimplement all of the LinkedList functions from scratch. However, since this is a beginner class, he has put a few stipulations on it. Below is the full assignment:
“For list, you must implement everything but get_allocator. For all functions that take an “iterator” as an argument, instead accept a node class. For all functions that return an iterator, return a node instead.”
Since LinkedList use pointers, I am not sure how to do return a node without just returning a pointer. What would be the syntax for returning a node pointed to by a pointer called *current?
I am also unsure how to format the functions. For example, what would be the syntax for changing the following function to accept a node class and then return a node(i don’t need the code for the function, just how to accept a node and return a node class):
iterator insert (iterator position, const int&x)
I think your professor is talking about accepting and returning pointers (or references) to nodes, not node values (copying nodes). I can say that with some degree of confidence because that’s the only way to preserve the underlying semantics of std::list with a one-to-one translation of iterators to nodes (node pointers).
However, if your nodes are just mere aggregates storing pointers themselves and don’t try to do any memory management, then you can get away with copying them around, but I’d go with pointers if in doubt.
So, something like this:
Would look like this for your practice homework:
If you aren’t using templates and can just make it a list of integers or whatever:
Hope later your professor will make you appreciate the power of the iterator-based versions as those allow you to write function templates that work not only on linked lists but any kind of sequence, for example.