-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReverseAnStack.cpp
More file actions
52 lines (51 loc) · 798 Bytes
/
Copy pathReverseAnStack.cpp
File metadata and controls
52 lines (51 loc) · 798 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
#include<iostream>
#include<stack>
#include<vector>
using namespace std;
void insert(stack<int>& st,int temp)
{
if (st.empty())
{
st.push(temp);
return;
}
int val = st.top();
st.pop();
insert(st,temp);
st.push(val);
return;
}
void reverse(stack<int>& st)
{
if(st.size()==1)
{
return;
}
int temp = st.top();
st.pop();
reverse(st);
insert(st,temp);
return;
}
int main() {
stack<int> st;
st.push(23);
st.push(24);
st.push(25);
st.push(26);
st.push(27);
if(st.empty()) {
return 0;
}
reverse(st);
cout<<st.top()<<endl;
st.pop();
cout<<st.top()<<endl;
st.pop();
cout<<st.top()<<endl;
st.pop();
cout<<st.top()<<endl;
st.pop();
cout<<st.top()<<endl;
return 0;
}