-
Notifications
You must be signed in to change notification settings - Fork 30
/
Reverse First K elements of Queue.cpp
75 lines (59 loc) · 1.63 KB
/
Reverse First K elements of Queue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Solution by Rahul Surana
***********************************************************
Given an integer K and a queue of integers, we need to reverse the order of the first K elements of the queue, leaving the other elements in the same relative order.
Only following standard operations are allowed on queue.
enqueue(x) : Add an item x to rear of queue
dequeue() : Remove an item from front of queue
size() : Returns number of elements in queue.
front() : Finds front item.
***********************************************************
*/
// { Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
queue<int> modifyQueue(queue<int> q, int k);
int main() {
int t;
cin >> t;
while (t-- > 0) {
int n, k;
cin >> n >> k;
queue<int> q;
while (n-- > 0) {
int a;
cin >> a;
q.push(a);
}
queue<int> ans = modifyQueue(q, k);
while (!ans.empty()) {
int a = ans.front();
ans.pop();
cout << a << " ";
}
cout << endl;
}
}// } Driver Code Ends
// User function Template for C++
// Function to reverse first k elements of a queue.
queue<int> modifyQueue(queue<int> q, int k) {
stack<int> s;
int i = 0;
while(i < k){
s.push(q.front());
q.pop();
i++;
}
int x = q.size();
while(!s.empty()) {
q.push(s.top());
s.pop();
}
while(x>0){
q.push(q.front());
q.pop();
x--;
}
return q;
}