我試圖在不同的堆棧中實現偶數或奇數的隊列&隊列。這裏是我的代碼:使用堆棧和隊列來分隔偶數和奇數的C++
如何顯示我的堆棧&隊列? 如何在奇數或偶數隊列中分開?
#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;
}
即使在棧和隊列奇!?目前還不清楚你試圖實現的目標 –
你不能迭代['std :: stack'](http://en.cppreference.com/w/cpp/container/stack)或['std ::隊列'](http://en.cppreference.com/w/cpp/container/queue),所以沒有辦法顯示它們的值而不從它們中刪除元素。 –
我創建了兩個堆棧和兩個隊列。我想在堆棧中添加所有的偶數和奇數。也希望與隊列相同。 –