-
Notifications
You must be signed in to change notification settings - Fork 0
/
tut148.c++
54 lines (46 loc) · 923 Bytes
/
tut148.c++
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
/*Reverse a stack using recursion -> Write a program to reverse a stack using recursion. You are not allowed to use loop constructs like while, for..etc, and you can only use the following ADT functions on Stack S:
isEmpty(S)
push(S)
pop(S)
*/
#include <bits/stdc++.h>
using namespace std;
void insert(stack<int> &s, int value)
{
if (s.size() == 0)
{
s.push(value);
return;
}
int b = s.top();
s.pop();
insert(s, value);
s.push(b);
}
void reverseStack(stack<int> &s)
{
if (s.size() == 0)
{
return;
}
int a = s.top();
s.pop();
reverseStack(s);
insert(s, a);
}
int main()
{
stack<int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
reverseStack(s);
cout << "After reversing the elements in the stack are: ";
while (!s.empty())
{
cout << s.top() << " ";
s.pop();
}
return 0;
}