forked from hardikagarwal2001/Hackoctober
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_queue_using_stack_algo.cpp
More file actions
64 lines (48 loc) · 908 Bytes
/
reverse_queue_using_stack_algo.cpp
File metadata and controls
64 lines (48 loc) · 908 Bytes
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
#include<bits/stdc++.h>
using namespace std;
#define int long long
stack<int> push_to_stack(queue<int> q){
stack<int> s;
while(!q.empty()){
s.push(q.front());
q.pop();
}
return s;
}
queue<int> push_back_to_queue(stack<int> s ){
queue<int> q;
while (!s.empty()) {
q.push(s.top()) ;
// cout<<s.top()<<endl;
s.pop();
}
return q;
}
void show(queue<int> gq)
{
queue<int> g = gq;
while (!g.empty()) {
cout << '\t' << g.front();
g.pop();
}
cout << '\n';
}
signed main(){
queue<int> q1;
int n;
cin>>n;
for (int i = 0; i < n; ++i)
{
/* code */
int x;
cin>>x;
q1.push(x);
}
cout<<"The queue elements before revrsing:"<<endl;
show(q1);
stack<int> s1 = push_to_stack(q1);
queue<int> q=push_back_to_queue(s1);
cout<<"The queue elements after revrsing:"<<endl;
show(q);
return 0;
}