The question:
Queue is a container of elements that are inserted and deleted according to FIFO (First In First Out).
For n queues Qi ( i = 0, 1, …, n-1 ), perform a sequence of the following operations.
I have three operations:
if q1=0, Insert an integer x to Qt
if q1=1, Report the value which should be deleted next from Qt . If Qt is empty, do nothing.
if q1=2,Delete an element from Qt . If Qt is empty, do nothing
In the initial state, all queues are empty.
my code is:
#include <bits/stdc++.h>
using namespace std;
#define fio
ios_base::sync_with_stdio(false);
cin.tie(NULL)
#define ll long long
int main()
{
fio;
ll n, q;
cin >> n >> q;
queue<pair<ll, ll>> qu;
while (q--)
{
ll q1, t, x;
cin >> q1 >> t;
if (q1 == 0)
{
cin >> x;
qu.push(make_pair(t, x));
}
else if (q1 == 1)
{
while (!qu.empty())
{
pair<ll, ll> f = qu.front();
if (t == f.first)
cout << f.second << endl;
}
qu.pop();
}
else if (q1 == 2)
{
while (!qu.empty())
{
qu.pop();
}
}
}
return 0;
}
input:3 9
0 0 1
0 0 2
0 0 3
0 2 4
0 2 5
1 0
1 2
2 0
1 0
output:
1
4
2
but I am getting 1,2,3. I had tried to use break but it was giving output: 1 4 but it was not working for last 1 0 value.
I have used a pair in queue that is storing values of t ,x .In condition q1==1 I have tried to match the values of first in pair with t. if it matches then I will print the second value of pair.
how can I fix it?