void NumberList::listDivisibleBy5or10()
{
ListNode* currentNode = head; //Pointer for current value in the link list
if(!head) // checks for an empty link list
{
std::cout<<"The list is empty \n"; // tells user that the link list is empty
}
else
{
while(currentNode)
{
if(currentNode->value /5 == 0)
{
currentNode->value /= 5;
}
else
currentNode = currentNode->next; // shows that the node after
// the current node is next
}
}
return;
}
void NumberList::listDivisibleBy5or10()
{
ListNode* currentNode = head; //Pointer for current value in the link list
if(!head) // checks for an empty link list
{
std::cout<<"The list is empty \n"; // tells user that the link list is empty
}
else
{
while(currentNode)
{
if(currentNode->value %5 == 0 && currentNode->value%10 == 0)
{
currentNode->value /= 5;
}
else
currentNode = currentNode->next; // shows that the node after
// the current node is next
}
}
return;
}
(1) When you traverse the list you MUST go on to the next element. Thus, the 'else' in the following block is wrong.
1 2 3 4 5 6 7
if(currentNode->value %5 == 0) // <==== see comment below about %
{
currentNode->value /= 5;
}
// else // <==== REMOVE THIS LINE
currentNode = currentNode->next; // <=== LEAVE THIS LINE - IT MUST RUN
(2) Test for divisibility with modulo % and not division / if(currentNode->value / 5 == 0)
should be if(currentNode->value % 5 == 0)
(3) You should have continued your previous thread, not started a new one. Then people would actually have (your teacher's) linked-list class to run the code and test it.