forked from hardikagarwal2001/Hackoctober
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackreversel
More file actions
64 lines (46 loc) · 733 Bytes
/
stackreversel
File metadata and controls
64 lines (46 loc) · 733 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
57
58
59
60
61
62
63
64
#include <bits/stdc++.h>
using namespace std;
void insert_at_bottom(stack<int>& st, int x)
{
if (st.size() == 0) {
st.push(x);
}
else {
int a = st.top();
st.pop();
insert_at_bottom(st, x);
st.push(a);
}
}
void reverse(stack<int>& st)
{
if (st.size() > 0) {
int x = st.top();
st.pop();
reverse(st);
insert_at_bottom(st, x);
}
return;
}
// Driver Code
int main()
{
stack<int> st, st2;
for (int i = 1; i <= 4; i++) {
st.push(i);
}
st2 = st;
cout << "Original Stack" << endl;
while (!st2.empty()) {
cout << st2.top() << " ";
st2.pop();
}
cout<<endl;
reverse(st);
cout << "Reversed Stack" << endl;
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
return 0;
}