forked from sarthakd999/Hacktoberfest2021-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse Stack.cpp
More file actions
47 lines (38 loc) · 718 Bytes
/
Reverse Stack.cpp
File metadata and controls
47 lines (38 loc) · 718 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
//C++ Program to reverse a stack without recursion
#include<bits/stdc++.h>
using namespace std;
void insertatbottom (stack<int> &st,int ele){
if(st.empty()){
st.push(ele);
return;
}
int topele=st.top();
st.pop();
insertatbottom(st,ele);
st.push(topele);
}
void reverse(stack <int> &st){
if(st.empty()){
return ;
}
int ele=st.top();
st.pop();
reverse(st);
insertatbottom(st,ele);
}
int main(){
stack<int> st;
int n;
cin>>n;
for(int i=0;i<n;i++){
int x;
cin>>x;
st.push(x);
}
reverse(st);
while(!st.empty()){
cout<<st.top()<<endl;
st.pop();
}
return 0;
}