I'm implementing a multi-branch tree with such a data structure.
Code:
typedef struct TreeNodeStruct
{
DataType m_data;
list<TreeNodeStruct*> m_children;
} TreeNode;
Sometimes I have to merge the children, just like this.
Code:
// merge the last child into the first child
root->m_children.front()->push_back(root->m_children.back());
root->m_children.pop_back();
But I soon found that the push_back() and pop_back() operations become too slow when the merging repeats many times. (Maybe these frequent operation is making the memory a mess?) So I'm wondering whether there is a way to "move" a node directly from one list to another so we do not have to call new() and delete() on memory frequently. Is STL providing such a way to do that? Or can we implement it ourselves?