The Link class member functions add, erase, find and advance. I need help with them. I wrote the helper function version of the functions, but I'm having modifying them for the member function version. Here's what I have right now:
Link* add(Link* p, Link* n) // insert n after p; return n
{
// much like insert (see exercise 11)
}
Link* erase(Link* p) // remove *p from list; return p’s successor
{
if (p==nullptr) returnnullptr;
if (p–>succ) p–>succ–>prev = p–>prev;
if (p–>prev) p–>prev–>succ = p–>succ;
return p–>succ;
}
Link* find(Link* p, const string& s) // find s in list;
// return nullptr for “not found”
{
while (p) {
if (p–>value == s) return p;
p = p–>succ;
}
returnnullptr;
}
Link* advance(Link* p, int n) // move n positions in list
// return nullptr for “not found”
// positive n moves forward, negative backward
{
if (p==nullptr) returnnullptr;
if (0<n){
while (n––){
if (p–>succ == nullptr) returnnullptr;
p = p–>succ;
}
}
elseif (n<0){
while (n++){
if (p–>prev == nullptr) returnnullptr;
p = p–>prev;
}
}
return p;
}
How should I modify these to the member functions where we have access to the this pointer? And wat would be the this pointer equivalent of the line p = p->prev;?
you need to differentiate a node from the list.
for example, ¿why does a node have a search function?, ¿what does advance mean? ¿are insert[i] and [i]erase responsibilities of a node or of a list?
> this == nullptr
that makes no sense, you are in a member function, ¿how may be this null then?
> this->succ;
statement has no effect (you probably have a warning with a similar message)
you are doing nothing there, no operation is performed.
Like I said, I need to know a good equivalent for the line p = p->prev; for that version of the function. And yeah, you're right about the text for null. I'll take that out.
This is part of a Link class. The rest of it, including the insert() function, were given in the book (I tried to modify that to write the add() function, but I'm not sure if I did it right). The book said at the end of the chapter that this is only the "link" part of a linked list. The "list" part will come later. It's like a re-implementation of the standard library "list" for teaching purposes.