Friday, February 15, 2008

How to implement a remove function within a stack similar to the STL implmentation:

iterator &remove(iterator& iter)
{
// Check if its good
if (!iter.good()) return iter;

// Get the node
Node* pDelete = iter.m_pCurr;

// Fix the stack's missing links
if (iter.m_pPrev != NULL)
iter.m_pPrev->pNext = iter.m_pCurr->pNext;
else
m_pTop = iter.m_pCurr->pNext;

if (iter.m_pPrev == NULL)
iter.m_pCurr = iter.m_pPrev;
else
iter.m_pCurr = iter.m_pPrev->pNext;

// Set the current to the next node
iter.m_pCurr = pDelete->pNext;

// Delete the data
delete pDelete;

// One less
--m_nListSize;

// Return the fixed iterator
return iter;
}

No comments: