[quote name='Robot Ninja' timestamp='1355607428' post='5011043']It seemed like a good idea for the Delete function in my linked list to return the node's data back to the user after deletion. However I'm not sure if it's even possible to return the data after, since my node's getter functions are returning by reference. Can you force a return-by-value from a return-by-reference function? Is there a better solution to this? Thanks.
That only sounds like a good idea if the lists job is just to maintain ordering, and not to own the memory management of the items. I.e. it would not actually do any deletion, just item removal. Otherwise the Delete function has no business giving anything back to the caller.
You can't change the behaviour of a reference.
We need more information about how the class works and what its purpose is to answer more thoroughly. I.e. is just the data part itself dynamically allocated? Does that happen outside or inside of the class? Just show more code rather than trying to answer these questions.
[/quote]
The linked list allocates the nodes dynamically, and is in fact an ordered list. The node itself contains the data, and only one next pointer, so obviously I'm dealing with a singly linked list. The list dynamically allocates the nodes inside its Insert function:
template <typename T>
void SortedList<T>::Insert( const T &item )
{
// Make new node the front if it has the 'lowest' value, or list is empty
if (m_pFront == nullptr || item < m_pFront->Data())
m_pFront = new ListNode(item, m_pFront);
else
{
ListNode *pLinkingNode = m_pFront; // newNode will be pointed to by this.
// Keep iterating until we find the 'in-order' pos, until we hit the end.
while (pLinkingNode->Next() != nullptr &&
pLinkingNode->Next()->Data() < item )
{
pLinkingNode = pLinkingNode->Next();
}
// Insert the node in its appropriate position
ListNode *pNewNode = new ListNode(item, pLinkingNode->Next());
pLinkingNode->SetNext(pNewNode);
}
m_count++;
}
Though it would be easier to use a doubly linked list, I created the class for a previous assignment and didn't want to revamp the list implementation when I started this assignment. I'm considering changing it now...
As a whole the list is supposed to serve my chained hash table, and then my hash table serves as an allocater for my custom language interpreter. I'm still figuring out how to tokenize the language syntax, but that topic should probably be in a different thread.