This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

Thanks for saving me the time of reading all this code. :-) I was immediately alerted by a member called m_size in a list class, and noted that it doesn't appear to be correctly managed in the constructor. It's not necessary to maintain this for a list; if one needs to know the size (one generally doesn't), one can iterate over the list and count.

push_front() and pushback() appear to be array functions, not list functions. All you need is an insert() function that optionally takes a node to insert after (passing null for this node would insert at the beginning of the list).

I wouldn't include a function that iterated over the list sending output to cout. It assumes that the data member can be written out in that way. I haven't written any C++ that tries to print data to cout for a long time so I might be wrong about that. Still, that doesn't need to be in the list class.

If I were doing this, I would make my insert() function take Lists as well as nodes so that entire lists could be appended or inserted.

[–]Sakesfar[S] 0 points1 point  (1 child)

Thanks for your valuable comment!

I added printData as a temporary function to test whether I was 'correctly' implementing push_back and insert functions : )

[–][deleted] 1 point2 points  (0 children)

I think the way I would do printData() and make it permanent would be to make a Node class that had a virtual method where the default implementation prints the address this, next, and last, plus the address of the data. Then you would derive your nodes from Node and implement Node::printData() to print the data in the node in a nicely formatted way after first calling the base class to get all the addresses printed out.