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
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.
// 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;
}