-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_stack.cpp
More file actions
56 lines (44 loc) · 905 Bytes
/
Copy pathreverse_stack.cpp
File metadata and controls
56 lines (44 loc) · 905 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
#include <iostream>
#include<stack>
using namespace std;
void insAtBottom(stack<int>& st,int topelement)
{
if(st.empty())
{
st.push(topelement);
return;
}
int element = st.top();
st.pop();
insAtBottom(st,topelement);
st.push(element);
}
void reverse(stack<int>& st)
{
if(st.empty())
return;
int topelement = st.top();
st.pop();
reverse(st);
insAtBottom(st,topelement);
}
int main()
{
stack<int> st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
st.push(6);
//Before reversing, if we print original stack then it will print-> 6 5 4 3 2 1 (because top is at 6)
//But after reversing, if we print original stack then it will print-> 1 2 3 4 5 6 (because now, top is at 1)
reverse(st);
cout<<"After Reversing: "<<endl;
while (!st.empty())
{
cout<<st.top()<<" ";
st.pop();
}
return 0;
}