#include <bits/stdc++.h>
using namespace std;
void printQueue(queue<int> q) {
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
}
void reverseQueueFirstNElements(queue<int> &q, int n) {
if (q.empty() == true || n > q.size() || n <= 0)
return;
stack<int> Stack;
for (int i = 0; i < n; i++) {
Stack.push(q.front());
q.pop();
}
while (!Stack.empty()) {
q.push(Stack.top());
Stack.pop();
}
for (int i = 0; i < q.size() - n; i++) {
q.push(q.front());
q.pop();
}
}
int main()
{
queue<int> q;
q.push(4);
q.push(2);
q.push(6);
q.push(1);
q.push(7);
q.push(5);
q.push(9);
printQueue(q);
reverseQueueFirstNElements(q, 4);
std::cout << '\n';
printQueue(q);
}
/*
run:
4 2 6 1 7 5 9
1 6 2 4 7 5 9
*/