forked from krishna14kant/Data-Structures-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMin_Stack.cpp
69 lines (43 loc) · 1.02 KB
/
Min_Stack.cpp
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
65
66
67
68
69
#include<bits/stdc++.h>
using namespace std;
stack<int>s;
stack<int>t;
void push(int val) {
s.push(val);
if(t.empty())
t.push(val);
else{
if(val<t.top())
t.push(val);
else
t.push(t.top());
}
}
void pop() {
s.pop();
t.pop();
}
int top() {
return s.top();
}
int getMin() {
if(t.empty())
return -1;
return t.top();
}
int main(){
cout<<"This program is used to find minimum value from stacks at any time without traversing stack\n\n";
cout<<"Enter number of Elements to push in stacks";
int n;
cin>>n;
for(int i=0;i<n;i++){
int num;
cin>>num;
push(num);
}
cout<<"The current minimum is : "<<getMin()<<endl;
cout<<"The top element is : "<<top()<<endl;
pop();
pop();
cout<<"The current minimum is : "<<getMin()<<endl;
}