Functions in a STACK header

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
void DynIntStack::OddOrEvenPrint()
{

	StackNode *currentNode;	
	StackNode *nextNode;	
	int stackOdd =0;		
	int stackEven = 0;
	
	if(isEmpty())
	{
		std::cout<<"The Stack is empty \n";
	}
	
	else
	{
	
		currentNode = top;
		while(currentNode!= NULL)
		{
			if(currentNode->value%2 == true)
			{
				stackEven++;
			}
			if(currentNode->value%2 == false)
			{
				stackOdd++;
			}
			currentNode = currentNode->next;
		
		}
		

	}
	std::cout<<"The number of odd numbers in the stack is: "<< stackOdd <<"\n";	
	std::cout<<"The number of even numbers in the stack is: "<< stackEven <<"\n";
}


Why is this giving me opposite results?

13 3 20
The number of odd numbers in the stack is: 1
The number of even numbers in the stack is: 2
Popping...
4 10 2
The number of odd numbers in the stack is: 3
The number of even numbers in the stack is: 0
Popping...
17 3 5
The number of odd numbers in the stack is: 0
The number of even numbers in the stack is: 3
Popping...
7
The number of odd numbers in the stack is: 0
The number of even numbers in the stack is: 1
Popping...
200
The number of odd numbers in the stack is: 1
The number of even numbers in the stack is: 0
Popping...

The Stack is empty
The number of odd numbers in the stack is: 0
The number of even numbers in the stack is: 0
Last edited on
If value == 5, what is value % 2? 1.
If value == 6, what is value % 2? 0.

The result of value % 2 is not a bool, it's an int. So don't compare it with a bool, it's just confusing. The bool is getting converted to an it, e.g. true becomes 1.
so you're essentially doing
if (value % 2 == 1) stackEven++;
which is mathematically wrong.

if value % 2 == 1, it's odd.
if value % 2 == 0, it's even.
Last edited on
That did the trick. Thank you so much.
For the reverse print function I have:

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
// implemented reverse print function
void DynIntStack::reversePrint()
{
StackNode *currentNode; //Pointer for current value in the stack
StackNode *nextNode; // Pointer for the next value in the stack
int stackSize = 0;
int temp;

 
if(isEmpty()) // checks for an empty stack
{
std::cout<<"The Stack is empty \n"; // tells user that the stack is empty
return;
}
 
currentNode = top;
while(currentNode!= NULL)
{
currentNode = currentNode->next; // shows that the node after the current node is next
stackSize++;
}

for(int pass=0; pass>stackSize; pass++)
{
currentNode = top;
nextNode = currentNode->next; // shows that the node after the current node is next
 
while(nextNode != NULL)
{
	if(currentNode -> value < nextNode->value)
	{
		temp = currentNode -> value;
		currentNode -> value= nextNode->value;
        nextNode->value= temp;  
    }
    
    currentNode = nextNode;
    nextNode = nextNode->next;
}
}
	displayList(); 
  	return;
}


BUT my output is
15 3 14
15 3 14
Popping...
3 14 15
3 14 15
Popping...
15 14 3
15 14 3
Popping...
3
3
Popping...

The Stack is empty


I want my output to be:
15 3 14
14 3 15
Popping...
3 14 15
15 14 3
Popping...
15 14 3
3 14 15
Popping...
3
3
Popping...

The Stack is empty
Topic archived. No new replies allowed.