-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseStack.cpp
More file actions
36 lines (27 loc) · 1001 Bytes
/
Copy pathreverseStack.cpp
File metadata and controls
36 lines (27 loc) · 1001 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
// top wala side me rakhenge pahle phir baki elemnts ko reverse kar dnege aur first wlae ko bottom me add kardenge
#include<iostream>
#include<stack>
using namespace std;
void insertAtBottom(stack<int> &s, int element) { // stack and size
// base class
if(s.empty()) { // empty tab hi toh moka hai push krne ka
s.push(element);
return;
}
int num = s.top();
s.pop();
// recursive call
insertAtBottom(s, element); // bottomme insert krenge
s.push(num); // push
}
void reverseStack(stack<int> &stack) {
// base class
if(stack.empty()) { // jab bhai stack empty hi hai toh kya reyrn kar do
return;
}
int num = stack.top(); // apneko pahla wala jo top pe hai usse side me rakhna hai abhi phir reverse krna hai bakike isko bottom me daln hai
stack.pop(); // top se pop kar diya
// recursive call
reverseStack(stack);
insertAtBottom(stack, num);
}