-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverseStackUsingRecursion.cpp
85 lines (74 loc) · 1.77 KB
/
reverseStackUsingRecursion.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
/*
Author : Shaurya Singhal(jugshaurya)
Date : 24-06-2022 T20
*/
// clang-format off
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vector<int>,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007 // 1e9+7
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define endl "\n"
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(_x) _x.begin(), _x.end()
#define sz(_x) int(_x.size())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
inline void fastIO(){
IOS
// for Google kickstart comment next line
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
// clang-format on
void insertAtBottom(stack<int> &s ,int val){
if(s.empty()){
s.push(val);
return;
}
int top = s.top(); s.pop();
insertAtBottom(s,val);
s.push(top);
}
void reverseAStack(stack<int> &s){
if(s.empty()) return;
int top = s.top(); s.pop();
reverseAStack(s);
insertAtBottom(s, top);
}
void print(stack<int> s){
cout<<endl;
while(!s.empty()){
cout<<s.top();
s.pop();
}
cout<<endl;
}
void solve(int _t) {
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
print(s);
reverseAStack(s);
print(s);
}
// clang-format off
signed main() {
fastIO();
int _t = 1;
// cin>>_t; for(int i=1;i<=_t;i++)
solve(_t);
return 0;
}