-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreverse_queue.cpp
109 lines (87 loc) · 1.62 KB
/
reverse_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
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
/*
ip: q = {10, 5, 15, 20}, front=10, rear=20
op: q = {20, 15, 5, 10}, front=20, rear=10
Iterative sol:
Traverse the queue
Put every item of queue into stack
so when we pop items from stack,
and enqueue them back to queue they will be reversed
Recursive sol:
If I am handling n-1 items
and there are total n items
how do I handle nth item?
we can dequeue nth item from the queue
reverse the rest of queue recursively
and then I push the nth item back in queue
eg: {12, 5, 15, 20}
front=12, rear=20
nth item = 12
dequeue nth item
q = {5, 15, 20}
reverse the rest
q = {20, 15, 5}
push the nth item back from rear end
q = {20, 15, 5, 12}
*/
void reverse_itr(queue<int> &q)
{
stack<int> s;
while(!q.empty())
{
s.push(q.front());
q.pop();
}
while(!s.empty())
{
q.push(s.top());
s.pop();
}
}
void reverse_rec(queue <int> &q)
{
if(q.empty())
return;
int x = q.front();
q.pop();
reverse_rec(q);
q.push(x);
}
int main()
{
queue<int> q;
q.push(12);
q.push(5);
q.push(15);
q.push(20);
cout << "Initial state of queue :: \n";
while(!q.empty())
{
cout << q.front() << ' ';
q.pop();
}
cout << "\n";
q.push(12);
q.push(5);
q.push(15);
q.push(20);
reverse_rec(q);
cout << "queue after reversal :: \n";
while(!q.empty())
{
cout << q.front() << " ";
q.pop();
}
cout << "\n";
return 0;
}
/*
op:
Initial state of queue ::
12 5 15 20
queue after reversal ::
20 15 5 12
*/