![]() When you push elements on you create them with new and when you pop elements off you delete them. I'm just curious to know if this is a good way of doing it?Ĭan we keep track of the number of elements in the stack using a count variable as used in the code?īasically your memory management is broken. This will cause problems if you try to copy list objects or initialize new ones with existing ones. This means that the pointer itself will be copied instead of just the data at that pointer. This is also important because the default copy constructor and assignment operator will perform a shallow copy of top. With the destructor defined, you'll need to satisfy The Rule of Three. There is no destructor! You're allocating new nodes, so you have to define the destructor to properly delete them: StackusingList::~StackusingList() ![]() StackusingList() should be an initializer list: StackusingList(int max) : top(NULL), maxnum(max), count(0) Prefer nullptr to NULL if you're using C++11. In your current implementation, node's fields are not hidden because they are all made public. It'd be better to define node as a struct inside StackusingList as private. This will allow you to store any date type, not just ints, in your structure. StackusingList *sl = new StackusingList(5) Īt some point, I'd recommend using template here. Can we keep track of the number of elements in the stack using a count variable as used in the code? #include I'm just curious to know if this is a good way of doing it. This is a working stack implementation using a linked list.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |