-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Reversing_the_first_K_elements_of_a_Queue.cpp
96 lines (74 loc) · 1.57 KB
/
Reversing_the_first_K_elements_of_a_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* Reversing the first K elements of a Queue */
#include<bits/stdc++.h>
using namespace std;
/* Function for Reversing the first K elements of a Queue */
void solve()
{
/* Input n = size of array */
int n;
cin >> n;
int a[n];
/* Declear the empty queue */
queue<int> q;
/* Input values in an array and
push array element into the queue */
for (int i = 0; i < n; ++i)
{
cin >> a[i];
q.push(a[i]);
}
/* Input k*/
int k;
cin >> k;
/* Declear a empty Stack */
stack<int> s;
/* Push k element in the Stack
from queue */
for (int i = 0; i < k; ++i)
{
s.push(q.front());
q.pop();
}
/* Enqueue the contents of stack
at the back of the queue */
while (!s.empty())
{
q.push(s.top());
s.pop();
}
/* Remove the remaining elements and
enqueue them at the end of the queue */
for (int i = 0; i < q.size() - k; i++)
{
q.push(q.front());
q.pop();
}
/* Print the queue */
cout << "After Reversing : ";
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
}
int main()
{
solve();
return 0;
}
/*
Test cases :
Input 1 :
10
10 20 30 40 50 60 70 80 90 100
5
Output 1 :
After Reversing : 50 40 30 20 10 60 70 80 90 100
Input 2 :
6
1 2 3 4 5 6
4
Output 2 :
After Reversing : 4 3 2 1 5 6
Time complexity: O(n)
Space Complexity: O(n)
*/