0

Im trying to implement the Even or Odd numbers in different stacks & queues. Here is my code:

How I can display my Stack & Queue? How I can separate in Odd or Even in any queue?

#include <iostream>
#include <stack>
#include <queue>
using namespace std;

int main()
{
stack <int> s1;
queue <int> q1;
int num[10]={0};

for(int i = 0; i < 10; i++)
{
    cout << "Enter Number " << i << ": ";
    cin >> num[i];

    s1.push(num[i]);
}

int s2;
int q2;
cout << "In Stack" << "\t" << "In Queue" << endl;

while(!q1.empty())
{
    for(int i = 0; i <10; i++)
    {
        if(num[i]%2 == 0)
        {
            s2 = s1.top();
            s1.pop();
        }
        else
        {
            q2 = q1.front();
            q1.pop();
        }
    }
    cout << s2 << "\t\t" << q2 << endl;
}

 return 0;
}
  • 4
    Even in a stack and Odd in a queue!? It's not clear what you are trying to achieve – Khalil Khalaf Apr 08 '16 at 18:18
  • 2
    You can't iterate over a [`std::stack`](http://en.cppreference.com/w/cpp/container/stack) or [`std::queue`](http://en.cppreference.com/w/cpp/container/queue) directly, so there's no way of displaying their values without removing the elements from them. – Some programmer dude Apr 08 '16 at 18:21
  • I create two stacks and two queues. I want to add all even and odd numbers in a stack. Also want the same with a queue. – Gabriel Valedon Apr 08 '16 at 18:22
  • So you have four in total? I assume you are trying to divide odd/even into two stacks and two queues. Then check the below answer. – Khalil Khalaf Apr 08 '16 at 18:54

1 Answers1

0

As I commented, I assume you want two stacks and two queues. Odd will go to an odd stack container and an odd queue container. Even will go to an even stack container and an even queue container as well.

This should work:

#include <stack>
#include <queue>

int main()
{
    std::stack<int> MyOddStack;
    std::queue<int> MyOddQueue;

    std::stack<int> MyEvenStack;
    std::queue<int> MyEvenQueue;

    int MyNumbers[10];
    int InNum;

    for (int i = 0; i < 10; i++) // take numbers from user and fill the container, the queue and the stack right away
    {
        std::cout << "Please enter number: " << std::endl;
        std::cin >> InNum;

        MyNumbers[i] = InNum; // put in the container

        if (InNum % 2 == 0) // if even add to even queue and even stack
        {
            MyEvenQueue.push(InNum);
            MyEvenStack.push(InNum);
        }
        else //else, add to odd queue and odd stack
        {
            MyOddQueue.push(InNum);
            MyOddStack.push(InNum);
        }
    }

    // You want to display any of the queues/stacks? 
    // put a for loop
    // use .top() and/or .front() to display
    // use .pop() everytime you display an element so you see the next element

    return 0;

}
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • Yes I wanna display it, but how I do it? I have an idea but I confused the .pop .front .front .back etc... – Gabriel Valedon Apr 09 '16 at 01:22
  • @GabrielValedon For the stack: see here http://stackoverflow.com/questions/12631514/how-can-i-print-out-the-contents-of-stdstack-and-return-its-size and use the same technique but .front() instead of .top() – Khalil Khalaf Apr 09 '16 at 17:24