-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack-reverseStack.cpp
More file actions
61 lines (46 loc) · 900 Bytes
/
Stack-reverseStack.cpp
File metadata and controls
61 lines (46 loc) · 900 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
#include<iostream>
#include<stack>
using namespace std;
void insertAtBottom(stack<int> &s, int element){
//base case
if(s.empty()){
s.push(element);
}
int num= s.top();
s.pop();
//recursive call
insertAtBottom(s, element);
s.push(num);
}
void reverseStack(stack<int> &s){
//base case
if(s.empty()){
return ;
}
int num= s.top();
s.pop();
//recursive call
reverseStack(s);
insertAtBottom(s, num);
return ;
}
int main(){
stack<int> s, s1;
s.push(2);
s.push(3);
s.push(4);
s.push(7);
s.push(6);
s.push(5);
s1= s;
while (!s1.empty()) {
cout << s1.top() << " ";
s1.pop();
}
reverseStack(s);
while (!s.empty()) {
cout << s.top() << " ";
s.pop();
}
return 0;
}