That is the idea I had, but obviously isn't working because of so many attempts at wrong conversions.
My thought was to make a tmp pointer that will traverse the second list, and use the addToTail function (already defined) to get the info from the tmp pointer to the addtoTail function.
We don't know much about intSLList and intSLLNode. Class definitions, please.
Is this how append() should work?
1 2 3 4 5 6 7 8 9 10 11 12
intSLList foo;
intSLList bar;
// add nodes to foo and bar
size_t f = foo.size(); // or whatever counts the nodes of a list
size_t b = bar.size();
foo.append( bar ); // move nodes of bar into tail of foo
assert( f + b == foo.size() );
assert( 0 == bar.size() );
class IntSLLNode {
public:
IntSLLNode() {
next = 0;
}
IntSLLNode(int el, IntSLLNode *ptr = 0) {
info = el; next = ptr;
}
int info;
IntSLLNode *next;
};
class IntSLList {
public:
IntSLList() {
head = tail = 0;
}
~IntSLList();
int isEmpty() {
return head == 0;
}
I need to append list 2 to list 1 and then empty out list 2.
That sounds like
1 2
addToTail(l2.head);
l2.head = nullptr;
Make sure that addToTail() is prepared to add a whole list and not just a single node. What I Really mean is that it must adjust the tail pointer to the end of the whole list.
By the way, it's very common to forget to update the tail pointer when writing list code. Every place where you can update head, there is probably a case where you must update tail also.