summaryrefslogtreecommitdiff
path: root/1_anno/Programmazione_2/data_structures/stack_w_array.cc
blob: c20db90041e85159aa7239e05fcdbd95b8bf1c69 (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
#include<iostream>

using namespace std;

template<class H>
class stack {
public:
    stack(int n) : _size{n}, _top{-1} {
        _arr = new H[n];
    }
    ~stack() {
        delete _arr;
    }
    bool is_empty() {
        return _top == -1;
    }
    bool is_full() {
        return _top == _size-1;
    }
    stack<H>* push(H x) {
        if(!is_full()) {
            _arr[++_top] = x;
        }
        return this;
    }
    H pop() {
        if(is_empty()) 
            return -1;
        _top--;
        return _arr[_top+1];
    }
private:
    int _size;
    H* _arr;
    short _top;
};

int main() {
    stack<int>* s = new stack<int>{7};

    s->push(15)->push(6)->push(2)->push(9)->push(17)->push(3);
    cout << s->pop() << '\n';
    s->push(18)->push(19)->push(12);

    for(int i = 0; i < 7; ++i)
        cout << s->pop() << ' ';

    delete s;
    return 0;
}