From d2edbc38cac8da52f58c5cd3da6c0c625fa05736 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sat, 6 Feb 2021 19:56:36 +0100 Subject: conf: rename --- .../Programming_2/data_structures/stack_w_array.cc | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Year_1/Programming_2/data_structures/stack_w_array.cc (limited to 'Year_1/Programming_2/data_structures/stack_w_array.cc') diff --git a/Year_1/Programming_2/data_structures/stack_w_array.cc b/Year_1/Programming_2/data_structures/stack_w_array.cc new file mode 100644 index 0000000..c20db90 --- /dev/null +++ b/Year_1/Programming_2/data_structures/stack_w_array.cc @@ -0,0 +1,50 @@ +#include + +using namespace std; + +template +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* 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* s = new stack{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; +} -- cgit v1.2.3-18-g5258