c++ - In a linked list, in what order are nodes removed by the destructor? -
this question has answer here:
- stl containers element destruction order 2 answers
in linked list, in order nodes removed destructor?
does go first last or last first?
either order possible -- way inspect specific implementation you're using.
in general, singly-linked list, expect first-to-last ordering because it's easier implement , bit more efficient:
linkedlist::~linkedlist() { node *node = mhead; while (node) { node *next = node->mnext; delete node; node = next; } }
versus last-to-first ordering, singly-linked list require sort of recursion:
void deletelist(node *node) { if (node == 0) { return; } deletelist(node->mnext); delete node; return; } linkedlist::~linkedlist() { deletelist(mhead); }
so again -- way sure @ linked list implementation.
Comments
Post a Comment