-
Notifications
You must be signed in to change notification settings - Fork 1
/
min_in_stack.cpp
97 lines (82 loc) · 1.72 KB
/
min_in_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
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
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
class Stack {
int *stack;
int top;
int size;
public:
Stack(int size) {
stack = (int *)malloc(sizeof(int) * size);
top = 0;
this->size = size;
}
bool push(int val) {
if (top==size)
return false;
else {
stack[top] = val;
top++;
return true;
}
}
int pop() {
if(isempty())
return -1;
else {
top--;
return stack[top];
}
}
bool isempty() {
return (top==0);
}
int peek() {
if (isempty())
return -1;
return stack[top-1];
}
};
class MinStack {
Stack mainstack(2), minstack(22);
public:
MinStack(int siz) {
// mainstack= Stack(3);
// Stack minstack = Stack(3);
}
/* bool push(int val) {
if (!this->mainstack.push(val))
return false;
if(this->minstack.isempty())
this->minstack.push(val);
else if(this->minstack.peek()>=val)
this->minstack.push(val);
}
int pop() {
if(minstack.peek()==mainstack.peek())
minstack.pop();
return mainstack.pop();
}
int min() {
if (minstack.isempty())
return -1;
else
return minstack.peek();
}
bool isempty() {
return mainstack.isempty();
}*/
};
int main() {
/* MinStack ms = MinStack(4);
ms.push(5);
ms.push(4);
ms.push(6);
ms.push(4);
ms.push(8);
for(int i=0; i<5; i++)
cout<<ms.pop()<<" "<<ms.min()<<" "<<ms.isempty()<<endl;
*/
return 0;
}