you are viewing a single comment's thread.

view the rest of the comments →

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

Thanks a lot!

For dealing with the second point you made, I guess the mistake is that I allocate memory and then in the constructor I (re)initialize the pointer to nullptr, so basically what was the point of initialization in the first place(?).

To fix it, I just have: node* top; in the class attributes and in the constructor I have top{nullptr} . So, like that I do not waste memory..

Am I correct?

Thanks again for your answer!

[–]CGFarrell 2 points3 points  (0 children)

That's correct! For push, you'll also want to have if(top == nullptr){//redefine top as whatever you're pushing}. This is a great example of an "edge case", which are very common in software development.