What I'm trying to do is sort a number of nodes of a linked list by a double value in the structure. However the program keeps crashing at the end of the sort function.
void scholarshipList::sortedRecords()
{
scholarshipNode *newHead, *currentNode, *previousNode, *lowestNode, *lowestPreviousNode;
newHead = head;
head = NULL;
double lowestAmount;
while (newHead != NULL) //While there is still a node under the head.
{
currentNode = newHead->next;
lowestAmount = currentNode->amount;
lowestNode = currentNode;
previousNode = newHead;
while (currentNode->next) //While there is still a node under the current node.
{
previousNode = currentNode;
currentNode = currentNode->next;
if(currentNode->amount < lowestAmount)
{
lowestPreviousNode = previousNode;
lowestAmount = currentNode->amount;
lowestNode = currentNode;
}
}
if (currentNode == newHead)
{
newHead = NULL;
}
lowestPreviousNode->next = lowestNode->next;
lowestNode->next = head->next; // The problem is one
head->next = lowestNode; // of these two lines I believe
}
displayAll();
}
Any help on getting this fixed would be much appreciated.