Adding function to header for link list

For this function I need it to check to see if the a value in a list is divisible by 5 and 10 and if true then the value gets divided by 5:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
}


I seriously need help.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
}
This is my source:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//This program demonstrates the append and displayList member functions

#include <iostream>
#include <iomanip>
#include "LinkList.h"
using namespace std;

int main()
{
	
	//Define a NumberList System
	NumberList list;
	//Test 1

	list.appendNode(10);
	list.appendNode(5);
	list.appendNode(5);
	list.appendNode(4);
	list.appendNode(100);
	list.appendNode(1);
	list.displayList();
	list.listDivisibleBy5or10();
	list.displayList();
	list.deleteList();
	list.displayList();
	
	//Test 2
	list.appendNode(1);
	list.appendNode(3);
	list.appendNode(6);
	list.appendNode(7);
	list.appendNode(9);
	list.displayList();
	list.listDivisibleBy5or10();
	list.displayList();
	list.deleteList();
	list.displayList();
	
	
	//Test 3
	list.appendNode(20);
	list.appendNode(40);
	list.appendNode(60);
	list.appendNode(80);
	list.appendNode(0);
	list.displayList();
	list.listDivisibleBy5or10();
	list.displayList();
	list.deleteList();
	list.displayList();
	
	//Test 3
	list.appendNode(5);
	list.appendNode(45);
	list.appendNode(65);
	list.appendNode(85);
	list.appendNode(0);
	list.displayList();
	list.listDivisibleBy5or10();
	list.displayList();
	list.deleteList();
	list.displayList();
	
	//Test4
	list.appendNode(5);
	list.displayList();
	list.listDivisibleBy5or10();
	list.displayList();
	list.deleteList();
	list.displayList();
	
	//Test5
	list.appendNode(10);
	list.displayList();
	list.listDivisibleBy5or10();
	list.displayList();
	list.deleteList();
	list.displayList();
	
	//Test6
	list.appendNode(1);
	list.displayList();
	list.listDivisibleBy5or10();
	list.displayList();
	list.deleteList();
	list.displayList();
	
	//Test7
	list.displayList();
	list.listDivisibleBy5or10();
	list.displayList();
	list.deleteList();
	list.displayList();
	
	system("pause");
	return 0;
	


}


However my output isn't showing any results. It just prints the first list.
I see three major mistakes.

(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.
Last edited on
Topic archived. No new replies allowed.