I have tested it with debugger and it seems to be linking fine. I just want to make sure I'm not unnecessarily allocating more memory than I should and that I'm releasing pointers properly. Basically how I wanted it to look is to have a cyclic infinite menu. So the last menu item points to the first one. I wasn't sure whether or not I did it correctly with the root node. SO
For my MenuItem class, I have 3 members:
Code: Select all
MenuItem* next;
MenuItem* previous;
std::string label;
The green arrow represents NEXT, the blue arrow represents PREVIOUS.
So the previous of root is always a nullptr.
The next of root always points to the first item in the menu.
The next of the last item in the menu always points to the first item in the menu
This is my code implementation: http://pastebin.com/3gvxzATU
I wasn't sure how to go about deleting the pointers. Do they not get deleted automatically by garbage collection? Do I have to do it manually? If I delete the first and current pointers at the end of the else statement, the code won't work (Throws bad alloc or something).
So is this correct or do I need to change something? Is there a better way of doing this?
EDIT:
And yes, adding a new item should add it as the first item, so to the very top of the menu.
EDIT 2:
I have updated the code. I was told, I don't need to make current and first on the heap. Also I didn't need current to traverse the menu at all! Here's the new code: http://pastebin.com/hPYtk5sL