-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC15CodingInterview.cpp
More file actions
75 lines (70 loc) · 1.12 KB
/
Copy pathC15CodingInterview.cpp
File metadata and controls
75 lines (70 loc) · 1.12 KB
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
70
71
72
73
74
75
/*
Author : Raunak Bhansali
Programm to successfully implement Extract Min operation in a stack in O(1) time Complexity
*/
#include<bits/stdc++.h>
using namespace std;
class MinStack{
stack<int> stk;
stack<int> stkMin;
static int count;
public:
bool push(int data)
{
bool val = false;
if(stkMin.empty() || data<stkMin.top())
{
stkMin.push(data);
}
else
stkMin.push(stkMin.top());
stk.push(data);
val = true;
return val;
}
int pop()
{
if(stk.empty())
return -1;
int d = stk.top();
stkMin.pop();
return d;
}
int peekMin()
{
if(stk.empty())
{
cout<<"Stack Empty"<<endl;
return -1;
}
return stkMin.top();
}
};
int main()
{
int T = 1;MinStack st;
while(T)
{
if(T==1)
{
cout<<"Enter A Element";
int d;
cin>>d;
st.push(d);
}
if(T==2)
{
if(st.pop()>=0)
cout<<"Element Popped";
}
if(T==3)
{
int q = st.peekMin();
if(q>=0)
cout<<"Min Of Stack = "<<q;
}
cout<<endl;
cout<<"Enter 1 to add 2 to pop 3 to peek"<<endl;
cin>>T;
}
}