forked from zapellass123/OpenEmailGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reverse_stack.cpp
48 lines (47 loc) · 857 Bytes
/
reverse_stack.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
#include<bits/stdc++.h>
#define lli long long int
#define ff first
#define ss second
#define vi vector<int>
#define REP(i,n) for(int i=0; i<n; i++)
using namespace std;
void insert(stack<int> &s,int val){
if(s.empty()){
s.push(val);
return;
}
int k=s.top();
s.pop();
insert(s,val);
s.push(k);
return;
}
void reverse(stack<int> &s){
if(s.empty()){
return;
}
int val=s.top();
s.pop();
reverse(s);
insert(s,val);
}
void printreverse(stack<int> &s){
while(!s.empty()){
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
}
int main(){
int n;
cin>>n;
stack<int> s;
for(int i=0; i<n; i++){
int x; cin>>x;
s.push(x);
}
reverse(s);
cout<<"after reversing the stack elements"<<endl;
printreverse(s);
cout<<endl;
}