-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_a_queue.cpp
More file actions
129 lines (99 loc) · 3 KB
/
reverse_a_queue.cpp
File metadata and controls
129 lines (99 loc) · 3 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
// Queue Reversal
// link - https://www.geeksforgeeks.org/problems/queue-reversal/1
/*
Given a queue q containing integer elements, your task is to reverse the queue.
Examples:
Input: q[] = [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.
Input: q[] = [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.
Input: q[] = [7, 9, 5, 12, 8]
Output: [8, 12, 5, 9, 7]
Explanation: After reversing the given elements of the queue, the resultant queue will be 8, 12, 5, 9, 7.
Constraints:
1 ≤ q.size() ≤ 106
1 ≤ q[i] ≤ 105
*/
// Approach 1: Using an auxiliary array
// Time Complexity: O(N), where N is the number of elements in the queue.
// Space Complexity: O(N) for the auxiliary array.
std::queue<int> reverseQueueWithArray(std::queue<int> &q) {
int s = q.size();
int * arr = new int[s];
int j = s-1;
while(!q.empty()){
arr[j] = q.front();
q.pop();
j--;
}
for(int i = 0; i < s; i++){
q.push(arr[i]);
}
delete [] arr;
return q;
}
// Approach 2: Using a stack
// Time Complexity: O(N), where N is the number of elements in the queue.
// Space Complexity: O(N) for the auxiliary stack.
std::queue<int> reverseQueue(std::queue<int> &q) {
if (q.empty()) return q;
int f = q.front(); // store the front
q.pop();
reverseQueue(q); // reverse the rest
q.push(f); // push the front to back
return q;
}
// Approach 3: Using Recursion
// Time Complexity: O(N) due to the recursive calls and pushing elements back.
// Space Complexity: O(N) for the recursion call stack.
std::queue<int> reverseQueueWithStack(std::queue<int>& q) {
std::stack<int> st;
while (!q.empty()) {
st.push(q.front());
q.pop();
}
while (!st.empty()) {
q.push(st.top());
st.pop();
}
return q;
}
std::queue<int> reverseQueueusingrecursion(std::queue<int> &q) {
if (q.empty()) return q;
int f = q.front(); // store the front
q.pop();
reverseQueue(q); // reverse the rest
q.push(f); // push the front to back
return q;
}
void printQueue(std::queue<int> q) {
while (!q.empty()) {
std::cout << q.front() << " ";
q.pop();
}
std::cout << std::endl;
}
int main() {
// Create a queue
std::queue<int> q;
q.push(4);
q.push(3);
q.push(1);
q.push(10);
q.push(2);
q.push(6);
std::cout << "Original queue: ";
printQueue(q);
// Reverse the queue using the stack approach
// Reverse the queue using the recursive approach
q = reverseQueueusingrecursion(q);
std::cout << "Reversed queue: ";
printQueue(q);
return 0;
}