aboutsummaryrefslogtreecommitdiff
path: root/challenge-095/paulo-custodio/cpp/ch-2.cpp
blob: 9606ea5a034706eaf2d76392e3fed198137366cc (plain)
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
/*
Challenge 095

TASK #2 › Demo Stack
Submitted by: Mohammad S Anwar
Write a script to demonstrate Stack operations like below:

push($n) - add $n to the stack
pop() - remove the top element
top() - get the top element
min() - return the minimum element

Example:
my $stack = Stack->new;
$stack->push(2);
$stack->push(-1);
$stack->push(0);
$stack->pop;       # removes 0
print $stack->top; # prints -1
$stack->push(0);
print $stack->min; # prints -1
*/

#include <iostream>
#include <vector>

class Stack {
public:
    void push(int n) { data.push_back(n); }
    void pop() { data.pop_back(); }
    int top() { return data.back(); }
    int min() {
        int m = data.front();
        for (auto& d : data)
            if (m > d)
                m = d;
        return m;
    }
private:
    std::vector<int> data;
};

int main() {
    Stack s;                            // my $stack = Stack->new;
    s.push(2);                          // $stack->push(2);
    s.push(-1);                         // $stack->push(-1);
    s.push(0);                          // $stack->push(0);
    s.pop();                            // $stack->pop;       # removes 0
    std::cout << s.top() << std::endl;  // print $stack->top; # prints -1
    s.push(0);                          // $stack->push(0);
    std::cout << s.min() << std::endl;  // print $stack->min; # prints -1
}