I need to know a good way to define insert() and erase() as well, preferably a way that won't give me errors about accessing private members. And how is my current definition of insert() for when iterator::curr isn't private?
How should I check for whether a node was inserted or removed? Check whether the number of next or previous pointers in the Links decreased or increased?
If I use operator new to allocate memory for a node in insert(), shouldn't I call delete in erase()? So how should I definite erase() in that case (or is it better how it is now, but I should delete after erase() (and maybe return the "erased" node from erase()))? Also, I think I should add initialization, default and copy constructors for this as well. I think I'll make an "empty list" for the default, with first == last and begin() == end(). I don't know how to define a generic default for Elem, though. One that would work for a default constructor of any type (empty string for std::string, 0 for numeric types, etc.).
Possible (normal) constructor definition:
1 2 3 4 5 6 7 8
|
list(const Elem &v)
{
Link<Elem> *new_node = new Link<Elem>;
new_node->prev = nullptr;
new_node->succ = last;
new_node->val = val;
first = new_node;
}
|
Possible default constructor definition:
1 2 3 4 5 6
|
list()
{
first->succ = last;
last->prev = first;
first->val{};
}
|
I'd prefer an initializer list kind (i.e.
1 2 3 4 5
|
list(const Elem &val)
: first{initializer}, last{initializer}
{
// definition
}
|
), but I went with this way here because the initializer list one seemed complicated right now.