I'm having a problem with a function for my linked list. What it does is navigate through a given number of nodes, seperates the list at that point, and then returns a pointer to the new head of the second list. If the end of the list is reached before the given number is reach, then it is supposed to return NULL. The function seems to work properly unless the end of the list is reached. I'm getting an exception if the list is shorter than the given number. I've tried a few different things and I don't understand what's causing it. NextNode is the List* for the list.
Code:
List* breakList(List* NodeHead, int NumNodes)
{
List* NewNodeHead;
while(NodeHead->NextNode && NumNodes)
{
NodeHead = NodeHead->NextNode;
NumNodes--;
}
NewNodeHead = NodeHead->NextMenuLine;
NodeHead->NextNode = NULL;
return NewNodeHead;
}
edit: Using text as test data I found I was losing 1 node each call. I've changed the return