forked from super30admin/Design-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem2.cpp
135 lines (111 loc) · 2.43 KB
/
Problem2.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// Created by shazmaan on 7/1/2019.
//
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
class SpecialStack {
int top; int MinTop;
int counter = 0; int MinIndex=0;
int minCounter = 0;
public:
int a[MAX];// Maximum size of Stack
int mina[MAX];
int size = 0;
SpecialStack() { //Constructor here
}
bool push(int x);
int pop();
int peek();
bool isEmpty();
bool isFull();
int getMin();
};
bool SpecialStack::push(int x){
if(counter<MAX){
a[counter]=x;
if((size==0)||x<minCounter){
MinTop=x; mina[MinIndex]=x; MinIndex++;
minCounter=x;
}
// cout<<"counter : "<<counter<<endl;
top=x;
counter++; size++;
// cout<<"Count : "<<counter<<" Size : "<<size<<endl;
return true;
}
cout<<"Stack Overflow"<<endl;
return false;
//Your code here
//Check Stack overflow as well
}
int SpecialStack::pop(){
int ret =0;
if(size==0){
cout<<"Stack Underflow"<<endl;
}else{
counter--;
// cout<<"Pop counter : "<<counter<<endl;
ret = a[counter];
top=a[size-2];
size--;
if(ret==MinTop){
minCounter = mina[MinIndex-2];
MinTop = minCounter;
MinIndex--;
}
// cout<<"TOP : "<<top<<endl;
}
return ret;
//Your code here
//Check Stack Underflow as well
}
int SpecialStack::peek() {
if(size==0){
return 0;
}else{
return top;
}
//Your code here
//Check empty condition too
}
bool SpecialStack::isEmpty() {
if(size==0){
return true;
}
return false;
//Your code here
}
bool SpecialStack::isFull() {
if(size==MAX){
return true;
}
return false;
}
int SpecialStack::getMin(){
return minCounter;
}
// Driver program to test above functions
int main() {
class SpecialStack s;
s.push(15);
s.push(30);
s.push(10);
s.push(20);
// cout<<s.size<<endl;
// cout << s.pop() << " Popped from stack\n";
// s.push(30);
cout<<s.size<<endl;
cout<<s.getMin()<<endl;
cout << s.pop() << " Popped from stack\n";
cout<<s.getMin()<<endl;
cout << s.pop() << " Popped from stack\n";
cout<<s.getMin()<<endl;
s.push(10);
cout<<s.getMin()<<endl;
cout << s.pop() << " Popped from stack\n";
cout<<s.getMin()<<endl;
// cout<<s.peek();
// s.push(30);
return 0;
}