forked from sachuverma/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue Reversal.cpp
54 lines (47 loc) · 1000 Bytes
/
Queue Reversal.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
/*
Queue Reversal
==============
Given a Queue Q containing N elements. The task is to reverse the Queue. Your task is to complete the function rev(), that reverses the N elements of the queue.
Example 1:
Input:
6
4 3 1 10 2 6
Output:
6 2 10 1 3 4
Explanation:
After reversing the given
elements of the queue , the resultant
queue will be 6 2 10 1 3 4.
Example 2:
Input:
4
4 3 2 1
Output:
1 2 3 4
Explanation:
After reversing the given
elements of the queue , the resultant
queue will be 1 2 3 4.
Your Task:
You only need to complete the function rev that takes a queue as parameter and returns the reversed queue. The printing is done automatically by the driver code.
Expected Time Complexity : O(n)
Expected Auxilliary Space : O(n)
Constraints:
1 ≤ N ≤ 105
1 ≤ elements of Queue ≤ 105
*/
queue<int> rev(queue<int> q)
{
stack<int> s;
while (!q.empty())
{
s.push(q.front());
q.pop();
}
while (!s.empty())
{
q.push(s.top());
s.pop();
}
return q;
}